summaryrefslogtreecommitdiff
path: root/MyTurl.java
diff options
context:
space:
mode:
Diffstat (limited to 'MyTurl.java')
-rw-r--r--MyTurl.java46
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
10import java.net.URL;
11import java.net.HttpURLConnection;
12import java.net.URLEncoder;
13
14
15public 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}