Jsoup gets redirected URL

I am trying to get the actual (redirected) URL from the one provided by the shortened URL.

Take twitter url shortener for example. I can get a response object that also parsed it to get a document.

Response response = Jsoup.connect("http://t.co/i5dE1K4vSs") .followRedirects(true) //to follow redirects .execute(); 

Now, given one redirect, where to get the final URL? Any method or strategy to achieve this?

+7
java jsoup url-shortener
source share
2 answers

The Response object has a url () method, which should give you the final url. So you can do, for example,

 String url = "http://t.co/i5dE1K4vSs"; Response response = Jsoup.connect(url).followRedirects(true).execute(); System.out.println(response.url()) 

If you want to get intermediate redirects, you must go after the redirect, and then check the heading for β€œlocation”. For example,

 String url = "http://t.co/i5dE1K4vSs"; Response response = Jsoup.connect(url).followRedirects(false).execute(); System.out.println(response.header("location")); 

If it has multiple redirection, you need to recursively call the urls.

+13
source share

the code:

 String originalUrl = Jsoup.connect("http://t.co/i5dE1K4vSs") .followRedirects(true) //to follow redirects .execute().url().toExternalForm(); System.out.println(originalUrl); 

Output:

 http://ibnlive.in.com/news/messi-considered-move-to-arsenal/487799-5-21.html 

Explanation:

Since Connection.Response has Connection.Base as a superinterface, you can simply use its #url () method (and then use the URL object as you want.

+1
source share

All Articles