diff options
author | Mike Crute <mcrute@gmail.com> | 2009-12-01 10:11:58 -0500 |
---|---|---|
committer | Mike Crute <mcrute@gmail.com> | 2009-12-01 10:11:58 -0500 |
commit | c8d560969d979b7ed978cafde1a0501cab20eab5 (patch) | |
tree | 23c90d7639d60a10069d96c7be27c123fd238929 /MyTurl.java | |
download | myturl_clients-master.tar.bz2 myturl_clients-master.tar.xz myturl_clients-master.zip |
Diffstat (limited to 'MyTurl.java')
-rw-r--r-- | MyTurl.java | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/MyTurl.java b/MyTurl.java new file mode 100644 index 0000000..53125ed --- /dev/null +++ b/MyTurl.java | |||
@@ -0,0 +1,46 @@ | |||
1 | /* | ||
2 | * myturl.com URL Shortener (Java Client) | ||
3 | * by Mike Crute (mcrute@gmail.com) | ||
4 | * on December 1, 2009 | ||
5 | * | ||
6 | * compile with: javac MyTurl.java | ||
7 | * run with: java MyTurl | ||
8 | */ | ||
9 | |||
10 | import java.net.URL; | ||
11 | import java.net.HttpURLConnection; | ||
12 | import java.net.URLEncoder; | ||
13 | |||
14 | |||
15 | public class MyTurl | ||
16 | { | ||
17 | public static void blow_up(String message) | ||
18 | { | ||
19 | System.err.println(message); | ||
20 | System.exit(1); | ||
21 | } | ||
22 | |||
23 | |||
24 | public static void main(String[] args) | ||
25 | throws Exception | ||
26 | { | ||
27 | if (args.length < 1) { | ||
28 | blow_up("This program takes one argument, the URL."); | ||
29 | } | ||
30 | |||
31 | String target = URLEncoder.encode(args[0], "US-ASCII"); | ||
32 | URL myTurl = new URL("http://myturl.com/add-url?url=" + target); | ||
33 | HttpURLConnection conn = (HttpURLConnection)myTurl.openConnection(); | ||
34 | |||
35 | if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) { | ||
36 | blow_up("Couldn't contact the service!"); | ||
37 | } | ||
38 | |||
39 | String turl = conn.getHeaderField("X-MyTurl"); | ||
40 | if (turl == null) { | ||
41 | blow_up("No turl was generated. Check your URL."); | ||
42 | } | ||
43 | |||
44 | System.out.println(turl); | ||
45 | } | ||
46 | } | ||