/* * 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); } }