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



The Java HttpURLConnection getFollowRedirects() method returns the value of this HttpURLConnection's instanceFollowRedirects field.

Declaration

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

public boolean getInstanceFollowRedirects()

Parameters

NA

Return Value

the value of this HttpURLConnection's instanceFollowRedirects field.

Exception

NA

Example 1

The following example shows the usage of Java HttpURLConnection getInstanceFollowRedirects() 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 getInstanceFollowRedirects() 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 getInstanceFollowRedirects() 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