java how to find the redirected url of a url

java how to find the redirected url of a url  using -'java,url,http-headers'

I am accessing web pages through java as follows:

URLConnection con = url.openConnection();

But in some cases, a url redirects to another url. So I want to know the url to which the previous url redirected.

Below are the header fields that I got as a response:

null-->[HTTP/1.1 200 OK]
last-modified-->[Sat, 17 Apr 2010 13:45:35 GMT]
Date-->[Sat, 17 Apr 2010 13:45:35 GMT]
Expires-->[Sat, 17 Apr 2010 14:45:35 GMT]
Set-Cookie-->[cl_def_hp=copenhagen;; path=/; expires=Sun, 17     Apr 2011 13:45:35 GMT, cl_def_lang=en;; path=/; expires=Sun, 17 Apr 2011 13:45:35 GMT]
Content-Type-->[text/html; charset=iso-8859-1;]

So at present, I am constructing the redirected url from the value of the Set-Cookie header field. In the above case, the redirected url is

Is there any standard way through which I can determine which url the particular url is going to redirect.

I know that when a url redirects to other url, the server sends an intermediate response containing a Location header field that tells the redirected url but I am not receiving that intermediate response through the url.openConnection(); method.

asked Sep 15, 2015 by Peggy11Lmrgd
0 votes

4 Answers

0 votes

You need to cast the URLConnection to HttpURLConnection and instruct it to not follow the redirects by setting HttpURLConnection#setInstanceFollowRedirects() to false. You can also set it globally by HttpURLConnection#setFollowRedirects().

You only need to handle redirects yourself then. Check the response code by HttpURLConnection#getResponseCode(), grab the Location header by URLConnection#getHeaderField() and then fire a new HTTP request on it.

answered Sep 15, 2015 by RubinLieberm
0 votes

Simply call getUrl() on URLConnection instance after calling getInputStream():

URLConnection con = new URL( url ).openConnection();
System.out.println( "orignal url: " + con.getURL() );
System.out.println( "connected url: " + con.getURL() );
InputStream is = con.getInputStream();
System.out.println( "redirected url: " + con.getURL() );

If you need to know whether the redirection happened before actually getting it's contents, here is the sample code:

HttpURLConnection con = (HttpURLConnection)(new URL( url ).openConnection());
con.setInstanceFollowRedirects( false );
int responseCode = con.getResponseCode();
System.out.println( responseCode );
String location = con.getHeaderField( "Location" );
System.out.println( location );
answered Sep 15, 2015 by WenMeehan
0 votes

@balusC I did as you wrote . In my case , I've added cookie information to be able to reuse the session .

   // get the cookie if need
    String cookies = conn.getHeaderField("Set-Cookie");

    // open the new connnection again
    conn = (HttpURLConnection) new URL(newUrl).openConnection();
    conn.setRequestProperty("Cookie", cookies);
answered Sep 15, 2015 by JorNelms
0 votes

I'd actually suggest using a solid open-source library as an http client. If you take a look at http client by ASF you'll find life a lot easier. It is an easy-to-use,scalable and robust client for http.

answered Sep 15, 2015 by NatishaBuckl