Java - HttpURLConnection getFollowRedirects()
The Java HttpURLConnection getFollowRedirects() method returns a boolean flag indicating whether or not HTTP redirects (3xx) should be automatically followed.
Declaration
Following is the declaration for java.net.URLConnection.getFollowRedirects() method
public static boolean getFollowRedirects()
Parameters
NA
Return Value
true if HTTP redirects should be automatically followed, false if not.
Exception
NA
Example 1
The following example shows the usage of Java HttpURLConnection getFollowRedirects() 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 getFollowRedirects() 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 getFollowRedirects() 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.