Java HttpURLConnection 类

Java HttpURLConnection getFollowRedirects() 方法返回一个布尔标志,指示是否应自动遵循 HTTP 重定向 (3xx)。

声明

以下是 java.net.URLConnection.getFollowRedirects() 方法的声明

public static boolean getFollowRedirects()

参数

返回值

如果应自动遵循 HTTP 重定向,则为 true,否则为 false。

异常

示例 1

以下示例显示了使用 Java HttpURLConnection getFollowRedirects() 方法获取 https 协议的有效 url。在此示例中,我们将创建 URL 类的实例。使用 url.openConnection() 方法,我们获取 HttpURLConnection 实例。使用 getFollowRedirects(),我们检查 followRedircts 标志值是否打印相同。现在使用 setFollowRedirects() 方法,我们更改 followRedircts 标志的值并再次打印相同的值 -

package com.yxjc123;

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.yxjc123.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();
      }
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

输出

Connected.
getFollowRedirects: true
getFollowRedirects: false
Disconnected.

示例 2

以下示例显示使用 Java HttpURLConnection getFollowRedirects() 方法获取具有 http 协议的有效 url。在此示例中,我们将创建 URL 类的实例。使用 url.openConnection() 方法,我们获取 HttpURLConnection 实例。使用 getFollowRedirects(),我们检查 followRedircts 标志值是否打印相同。现在使用 setFollowRedirects() 方法,我们更改 followRedircts 标志的值并再次打印相同的值 -

package com.yxjc123;

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.yxjc123.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();
      }
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

输出

Connected.
getFollowRedirects: true
getFollowRedirects: false
Disconnected.

示例 3

以下示例显示使用 Java HttpURLConnection getFollowRedirects() 方法获取具有 http 协议的有效 url。在此示例中,我们将创建 URL 类的实例。使用 url.openConnection() 方法,我们获取 HttpURLConnection 实例。使用 getFollowRedirects(),我们检查 followRedircts 标志值是否打印相同。现在使用 setFollowRedirects() 方法,我们更改 followRedircts 标志的值并再次打印相同的值 -

package com.yxjc123;

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.baidu.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();
      }
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

输出

Connected.
getFollowRedirects: true
getFollowRedirects: false
Disconnected.