Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expanding a shortened url to its original full length url in java

I'm attempting to take a shortened url and expand it out to its original full length url in a string format in java. I've been able to track down a tutorial online, however I'm unable to get this to actually get me the full url. Has anyone done this before or know how to do this? Any help is huge, thanks

URLConnection conn = null;
try {
    URL inputURL = new URL("http://bit.ly/9mglq8");
    conn = inputURL.openConnection();

} catch (MalformedURLException e) {

} catch (IOException ioe) {

}
String realU = conn.toString();
Toast.makeText(ImagetestActivity.this, realU,
               Toast.LENGTH_LONG).show();
like image 760
Edmund Rojas Avatar asked May 19 '12 00:05

Edmund Rojas


People also ask

How do I get the original URL for a short URL?

Before You Click, Reveal Full URLs There are a number of ways you can reveal the full URL behind a shortened URL: Use the shortening service preview feature. Type the shortened URL in the address bar of your web browser and add the characters described below to see a preview of the full URL: tinyurl.com.

How do you shorten a long URL in Java?

One Simple Solution could be Hashing. Use a hash function to convert long string to short string. In hashing, that may be collisions (2 long URLs map to same short URL) and we need a unique short URL for every long URL so that we can access long URL back.


1 Answers

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;
}
like image 176
Satya Avatar answered Sep 27 '22 22:09

Satya