Main Tutorials

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 Author

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

Comments

Subscribe
Notify of
27 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Hojjat Imani
6 years ago

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)

sree.robosoft
10 years ago

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.

Mario La Menza Perello
9 years ago

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
7 years ago

Thanks a ton for this.

Dipali
1 year ago

Thanks. exactly I was looking for this,Its helpful.

Maurice
5 years ago

Helpful !! 100% Working code.

Patrick
5 years ago

Thanks exactly what i was looking for. It works for me.

Ram
5 years ago

I did the same code in android studio but still giving “org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject"

miked0dd
1 year ago
Reply to  Ram

Can’t be certain without looking at your Android code, but it sounds like you have a different problem. Seems as though your redirect worked; however, it appears your Android code it trying to cast an HTML response (a String instance) to a JSONObject instance.

Allanksr
5 years ago

Congratulations, It was exactly what I was trying to find. THANK YOU.

Himanshu Dharpure
7 years ago

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

Mehmet Ali Abdulhayoglu
9 years ago

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
9 years ago

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
9 years ago

Excellent tips here, nice job man

Rajkumar
10 years ago

thank you very much

Ndes
10 years ago

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
10 years ago

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
10 years ago

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
10 years ago

Nice! You just saved my night!! Thanks

vinay
10 years ago

Thank you very much for all your posts !!

satya
10 years ago

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
11 years ago

Very useful ! thanks !

Bhavesh
11 years ago

Thank You So Much.

Sudhir
11 years ago

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
11 years ago

Hi can you write tutorials on Hadoop and Map Reduce?

MITHUN
11 years ago

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

raju muddana
11 years ago

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.