Which library or built in functionality would allow you to take a shorted URL like bitly, fb.me, google's shortener, etc... and get its final link in the fastest time?
thanks
Which library or built in functionality would allow you to take a shorted URL like bitly, fb.me, google's shortener, etc... and get its final link in the fastest time?
thanks
programming time mostly, I'm using httpclient from apache right now wondering if their are any alternatives built in
There's java.net.URLConnection
.
String location = "http://tinyurl.com/so-hints";
HttpURLConnection connection = (HttpURLConnection) new URL(location).openConnection();
connection.setInstanceFollowRedirects(false);
while (connection.getResponseCode() / 100 == 3) {
location = connection.getHeaderField("location");
connection = (HttpURLConnection) new URL(location).openConnection();
}
System.out.println(location); // http://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx
This isn't a real answer, but you basically need to call the shortened URL via an HTTP Client, and then see what the 302 header in the response is. Then redirect the user to that URL.
(I wonder why you'd want to do this though, just send the shortened URL as a 302 redirect, and let them bounce again).
I was looking for the same and what BalusC suggested above helped. I have a fix here that acts as an enhancement actually.
String location = "http://j.mp/mAag6Q";
HttpURLConnection connection = (HttpURLConnection) new URL(location).openConnection();
connection.setInstanceFollowRedirects(false);
while (connection.getResponseCode() / 100 == 3) {
location = connection.getHeaderField("location");
System.out.println(location);
connection = (HttpURLConnection) new URL(location).openConnection();
**connection.setInstanceFollowRedirects(false);**
}
Basically all I did was to set the connection's setInstanceFollowRedirects to false again in a loop so it handles multiple level redirection as well.