Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Useful Resources

Java - HttpURLConnection setInstanceFollowRedirects() Method



The Java HttpURLConnection setInstanceFollowRedirects() method sets whether HTTP redirects (requests with response code 3xx) should be automatically followed by this HttpURLConnection instance.

The default value comes from followRedirects, which defaults to true.

Declaration

Following is the declaration for java.net.URLConnection.setInstanceFollowRedirects() method

public void setInstanceFollowRedirects(boolean followRedirects)

Parameters

followRedirects − a boolean indicating whether or not to follow HTTP redirects.

Return Value

the value of this HttpURLConnection's instanceFollowRedirects field.

Exception

NA

Example 1

The following example shows the usage of Java HttpURLConnection setInstanceFollowRedirects() method for a valid url with https protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the HttpURLConnection instance. Using getInstanceFollowRedirects(), we're checking the instanceFollowRedircts flag value printing the same. Now using setInstanceFollowRedirects() method, we're changing the value of instanceFollowRedircts flag and printing the same again −

package com.tutorialspoint;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://www.tutorialspoint.com/index.htm?language=en#j2se");
         HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
         urlConnection.connect();  
         System.out.println("Connected.");  

         boolean result = urlConnection.getInstanceFollowRedirects();
         System.out.println("getInstanceFollowRedirects: " + result);  

         urlConnection.setInstanceFollowRedirects(false);
         result = urlConnection.getInstanceFollowRedirects();
         System.out.println("getInstanceFollowRedirects: " + result); 

         urlConnection.disconnect();
         System.out.println("Disconnected.");  
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

Connected.
getInstanceFollowRedirects: true
getInstanceFollowRedirects: false
Disconnected.

Example 2

The following example shows the usage of Java HttpURLConnection setInstanceFollowRedirects() method for a valid url with http protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the HttpURLConnection instance. Using getInstanceFollowRedirects(), we're checking the instanceFollowRedircts flag value printing the same. Now using setInstanceFollowRedirects() method, we're changing the value of instanceFollowRedircts flag and printing the same again −

package com.tutorialspoint;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("http://www.tutorialspoint.com/index.htm?language=en#j2se");
         HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
         urlConnection.connect();  
         System.out.println("Connected.");  

         boolean result = urlConnection.getInstanceFollowRedirects();
         System.out.println("getInstanceFollowRedirects: " + result);  

         urlConnection.setInstanceFollowRedirects(false);
         result = urlConnection.getInstanceFollowRedirects();
         System.out.println("getInstanceFollowRedirects: " + result); 

         urlConnection.disconnect();
         System.out.println("Disconnected.");  
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

Connected.
getInstanceFollowRedirects: true
getInstanceFollowRedirects: false
Disconnected.

Example 2

The following example shows the usage of Java HttpURLConnection setInstanceFollowRedirects() method for a valid url with http protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the HttpURLConnection instance. Using getInstanceFollowRedirects(), we're checking the instanceFollowRedircts flag value printing the same. Now using setInstanceFollowRedirects() method, we're changing the value of instanceFollowRedircts flag and printing the same again −

package com.tutorialspoint;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("http://www.google.com");
         HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
         urlConnection.connect();  
         System.out.println("Connected.");  

         boolean result = urlConnection.getInstanceFollowRedirects();
         System.out.println("getInstanceFollowRedirects: " + result);  

         urlConnection.setInstanceFollowRedirects(false);
         result = urlConnection.getInstanceFollowRedirects();
         System.out.println("getInstanceFollowRedirects: " + result); 

         urlConnection.disconnect();
         System.out.println("Disconnected.");  
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

Connected.
getInstanceFollowRedirects: true
getInstanceFollowRedirects: false
Disconnected.
java_httpurlconnection.htm
Advertisements