Prevent Automatic Redirects in a HTTP Connection in Java

Description

The following code shows how to prevent Automatic Redirects in a HTTP Connection.

Example


import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
//from  w w w.  j  av a2  s . c o m
public class Main {

  public static void main(String args[]) throws Exception {
    HttpURLConnection.setFollowRedirects(false);

    URL url = new URL("http://www.google.com");
    URLConnection conn = url.openConnection();

    HttpURLConnection httpConn = (HttpURLConnection) conn;
    httpConn.setInstanceFollowRedirects(false);

    conn.connect();

  }

}




















Home »
  Java Tutorial »
    Network »




NetworkInterface
URI
URL
HTTP
HTTP Read
IP
Socket
UDP
URL Encode