Expanding a shortened url to its original full length url in java
java, url, urlconnection
Solution
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;
}
Problem
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(); ```