Java HttpURLConnection follow redirect example

301-redirect

The HttpURLConnection‘s follow redirect is just an indicator, in fact it won’t help you to do the “real” http redirection, you still need to handle it manually.


URL obj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setInstanceFollowRedirects(true);  //you still need to handle redirect manully.
HttpURLConnection.setFollowRedirects(true);

1. Java Http Redirect Example

If a server is redirected from the original URL to another URL, the response code should be 301: Moved Permanently or 302: Temporary Redirect. And you can get the new redirected url by reading the “Location” header of the HTTP response header.

For example, access to the normal HTTP twitter website – http://www.twitter.com , it will auto redirect to the HTTPS twitter website – https://www.twitter.com.

HttpRedirectExample – Full Java follow redirect example, see comments for self-explanatory.

package com.mkyong.http;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpRedirectExample {

  public static void main(String[] args) {

    try {

	String url = "http://www.twitter.com";

	URL obj = new URL(url);
	HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
	conn.setReadTimeout(5000);
	conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
	conn.addRequestProperty("User-Agent", "Mozilla");
	conn.addRequestProperty("Referer", "google.com");

	System.out.println("Request URL ... " + url);

	boolean redirect = false;

	// normally, 3xx is redirect
	int status = conn.getResponseCode();
	if (status != HttpURLConnection.HTTP_OK) {
		if (status == HttpURLConnection.HTTP_MOVED_TEMP
			|| status == HttpURLConnection.HTTP_MOVED_PERM
				|| status == HttpURLConnection.HTTP_SEE_OTHER)
		redirect = true;
	}

	System.out.println("Response Code ... " + status);

	if (redirect) {

		// get redirect url from "location" header field
		String newUrl = conn.getHeaderField("Location");

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

		// open the new connnection again
		conn = (HttpURLConnection) new URL(newUrl).openConnection();
		conn.setRequestProperty("Cookie", cookies);
		conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
		conn.addRequestProperty("User-Agent", "Mozilla");
		conn.addRequestProperty("Referer", "google.com");
								
		System.out.println("Redirect to URL : " + newUrl);

	}

	BufferedReader in = new BufferedReader(
                              new InputStreamReader(conn.getInputStream()));
	String inputLine;
	StringBuffer html = new StringBuffer();

	while ((inputLine = in.readLine()) != null) {
		html.append(inputLine);
	}
	in.close();

	System.out.println("URL Content... \n" + html.toString());
	System.out.println("Done");

    } catch (Exception e) {
	e.printStackTrace();
    }

  }

}

Output


Request URL ... http://www.twitter.com
Response Code ... 301
Redirect to URL : https://twitter.com/
URL Content... 
<!DOCTYPE html><!--[if IE 8]><html class= // twitter.com url content...

Do share if you have better way to handle the HTTP redirection :)

References

  1. HttpURLConnection JavaDoc
  2. HttpURLConnection Android JavaDoc, more examples
  3. Wiki : List of HTTP header fields

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Hojjat Imani
Guest
Hojjat Imani

HttpUrlConnection’s follow redirect is not only an indicator. it does really follow the http redirects. The only point is that it doesn’t follow redirects from a protocol to another (e.g. http to https and vice versa)
(this might be helpful https://stackoverflow.com/questions/1884230/urlconnection-doesnt-follow-redirect)

Himanshu Dharpure
Guest
Himanshu Dharpure

What if “String newUrl = conn.getHeaderField(“Location”);” gives a relative url not absolute one ?

Mario La Menza Perello
Guest
Mario La Menza Perello

Hi, Mkyong, HttpURLConnection follow automatically redirections (until five times). But don’t follow redirects from HTTPS to HTTP and viceversa (see http://developer.android.com/reference/java/net/HttpURLConnection.html).
Try to run your own code using http://www.google.com as url (which will return a 302) and you will get a 200 OK, but if you set conn.setInstanceFollowRedirects(false); you will be able to see the 302. Regards,

Utsav Saha
Guest
Utsav Saha

Thanks a ton for this.

Mehmet Ali Abdulhayoglu
Guest
Mehmet Ali Abdulhayoglu

Hi thanx for this useful information. I have an issue related with redirecting. I am trying to connect “http://pt.wkhealth.com/pt/re/lwwgateway/landingpage.htm;jsessionid=JQ6G1nm1K82pJdJMJgLbZ7zb8RvmhJH1pd08QSRdznrv2vWRR8PX!-1777306561!181195629!8091!-1?sid=WKPTLP:landingpage&an=00124278-201112000-00022”. It is redirecting to another url. However, when I use you script above, status is given as 200?! What am i missing? Thanks.

anis
Guest
anis

This post is helpful for me.i am able perform using google and twitter sites… but i got a SSLProtocolException when i try to connect through my java code when-
the url is like https://……../index.jsp
when i hit on web then it redirect me to https://……../login.jsp

exception:
javax.net.ssl.SSLProtocolException: handshake alert: unrecognized_name

but when i hit using java code then exception occur… why?

Alexandre
Guest
Alexandre

Excellent tips here, nice job man

Rajkumar
Guest
Rajkumar

thank you very much

Ndes
Guest
Ndes

Thanks for this. Had an issue though setting the header params on the redirect. When I set the header params they are set on the temporary response and not on the redirect itself. How can header params be set on the redirected URL.

Amruta
Guest
Amruta

I have been following your site for few months,the posts are so useful and simply explained. Now i have a requirement, I want to use httpurlconnection object for multiple connection,but I am facing problem. Can you please guide me? Thanks in advance.

Javier
Guest
Javier

Hi, I have a problem with apache redirections. The question is: if in web header I obtain code “302 Found” and process stop. and redirection can’t be complete. I trying go to the https from http.

Daniel
Guest
Daniel

Nice! You just saved my night!! Thanks

sree.robosoft
Guest
sree.robosoft

We need to set conn.setInstanceFollowRedirects(false); Unless it will follow the redirect. If you want the redirected url , we need to override the default property.

vinay
Guest
vinay

Thank you very much for all your posts !!

satya
Guest
satya

Is there a way to read multiple webpages of a same website. I am using java.net classes. I can successfully authenticate to a website and read the data from first web page. However, when I try to read the second web page of the same site the website is trying to authenticate again. How can I read the other web pages of the same site without re-authenticating again?
— Thanks
Satya

Ten mien
Guest
Ten mien

Very useful ! thanks !

Bhavesh
Guest
Bhavesh

Thank You So Much.

Sudhir
Guest
Sudhir

Dear Sir,
I am not able to open this website in my computer.
I had to go to my colleague’s system to read your articles.

Please help.

Thanks,
Sudhir

Naidu
Guest
Naidu

Hi can you write tutorials on Hadoop and Map Reduce?

MITHUN
Guest
MITHUN

I just read your blogg today.I am impressed to watch it.and I think I will always visit this site.thanks

raju muddana
Guest
raju muddana

I have been following your site for few months,the posts are so useful and simply explained. Now i have a requirement,i could not found solution any where?!.Now i would like to ask you to guide me.

REQUIREMENT: I have to extract published-time and article-content of a news article from news sites.For extracting article-content now i am using Boiler-pipe, But it works for only some posts(news artilce posts), and for published-time extractinon i have no solution.

Please help me to extract published-time and article content.And i am working on java.

Thanks&Regards.