Java HttpURLConnection follow redirect example

摘要: 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.

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

上一篇: Spring Data MongoDB : Save binary file, GridFS example
下一篇: Download JDK Source code for Mac OS X
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号