Java 教程

Java 控制語句

面向物件程式設計

Java 內建類

Java 檔案處理

Java 錯誤與異常

Java 多執行緒

Java 同步

Java 網路程式設計

Java 集合

Java 介面

Java 資料結構

Java 集合演算法

高階 Java

Java 雜項

Java APIs 和框架

Java 類引用

Java 有用資源

Java - HttpURLConnection getFollowRedirects()



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.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://tutorialspoint.tw/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.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://tutorialspoint.tw/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.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();
      }
   }
}

讓我們編譯並執行上述程式,這將產生以下結果:

輸出

Connected.
getFollowRedirects: true
getFollowRedirects: false
Disconnected.
java_httpurlconnection.htm
廣告
© . All rights reserved.