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 setFollowRedirects() Method



The Java HttpURLConnection setFollowRedirects() method sets whether HTTP redirects (requests with response code 3xx) should be automatically followed by this class. True by default. Applets cannot change this variable.

Declaration

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

public static void setFollowRedirects(boolean set)

Parameters

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

Return Value

NA

Exception

SecurityException − if a security manager exists and its checkSetFactory method doesn't allow the operation.

Example 1

The following example shows the usage of Java HttpURLConnection setFollowRedirects() 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 getFollowRedirects(), we're checking the followRedircts flag value printing the same. Now using setFollowRedirects() method, we're changing the value of followRedircts 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.getFollowRedirects();
         System.out.println("getFollowRedirects: " + result);  

         urlConnection.setFollowRedirects(false);
         result = urlConnection.getFollowRedirects();
         System.out.println("getFollowRedirects: " + 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.
getFollowRedirects: true
getFollowRedirects: false
Disconnected.

Example 2

The following example shows the usage of Java HttpURLConnection setFollowRedirects() 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 getFollowRedirects(), we're checking the followRedircts flag value printing the same. Now using setFollowRedirects() method, we're changing the value of followRedircts 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.getFollowRedirects();
         System.out.println("getFollowRedirects: " + result);  

         urlConnection.setFollowRedirects(false);
         result = urlConnection.getFollowRedirects();
         System.out.println("getFollowRedirects: " + 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.
getFollowRedirects: true
getFollowRedirects: false
Disconnected.

Example 3

The following example shows the usage of Java HttpURLConnection setFollowRedirects() 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 getFollowRedirects(), we're checking the followRedircts flag value printing the same. Now using setFollowRedirects() method, we're changing the value of followRedircts 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.getFollowRedirects();
         System.out.println("getFollowRedirects: " + result);  

         urlConnection.setFollowRedirects(false);
         result = urlConnection.getFollowRedirects();
         System.out.println("getFollowRedirects: " + 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.
getFollowRedirects: true
getFollowRedirects: false
Disconnected.
java_httpurlconnection.htm
Advertisements