Friday, September 3, 2010

Expanding short url to original url using Java

Sunday, May 10, 2009, 20:58
This news item was posted in Technology category and has 0 Comments so far.

There are plenty of URL shortening services designed for Twitter. Recently, Twitter picked bit.ly as their official URL shortening service. Here is quick Java code to expand short URL to original URL-

 

 

                System.out.println(”Short URL: “+ shortURL);
                urlConn =  connectURL(shortURL);
                urlConn.getHeaderFields();
                System.out.println(”Original URL: “+ urlConn.getURL());

// connectURL - This function will take a valid url and return a URL object representing the url address.

URLConnection connectURL(String strURL) {
        URLConnection conn =null;
 try {
     URL inputURL = new URL(strURL);
     conn = inputURL.openConnection();
            int test = 0;
         
 }catch(MalformedURLException e) {
     System.out.println(”Please input a valid URL”);
 }catch(IOException ioe) {
     System.out.println(”Can not connect to the URL”);
 }
       return conn;

}

Responses are currently closed, but you can trackback from your own site.

website design quote