blob: 53125edc66408d46a26b18259bc8f1dbab389a95 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
/*
* myturl.com URL Shortener (Java Client)
* by Mike Crute (mcrute@gmail.com)
* on December 1, 2009
*
* compile with: javac MyTurl.java
* run with: java MyTurl
*/
import java.net.URL;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
public class MyTurl
{
public static void blow_up(String message)
{
System.err.println(message);
System.exit(1);
}
public static void main(String[] args)
throws Exception
{
if (args.length < 1) {
blow_up("This program takes one argument, the URL.");
}
String target = URLEncoder.encode(args[0], "US-ASCII");
URL myTurl = new URL("http://myturl.com/add-url?url=" + target);
HttpURLConnection conn = (HttpURLConnection)myTurl.openConnection();
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
blow_up("Couldn't contact the service!");
}
String turl = conn.getHeaderField("X-MyTurl");
if (turl == null) {
blow_up("No turl was generated. Check your URL.");
}
System.out.println(turl);
}
}
|