Java 教程

Java 控制語句

面向物件程式設計

Java 內建類

Java 檔案處理

Java 錯誤與異常

Java 多執行緒

Java 同步

Java 網路程式設計

Java 集合

Java 介面

Java 資料結構

Java 集合演算法

高階 Java

Java 雜項

Java API與框架

Java 類參考

Java 有用資源

Java - URLConnection setAllowUserInteraction(boolean allowuserinteraction)



描述

Java URLConnection setAllowUserInteraction(boolean allowuserinteraction) 方法設定此物件的 allowUserInteraction 欄位的值。

宣告

以下是 java.net.URLConnection.setAllowUserInteraction(boolean allowuserinteraction) 方法的宣告

public void setAllowUserInteraction(boolean allowuserinteraction)

引數

allowuserinteraction − 新值。

返回值

異常

IllegalStateException − 如果已經連線

示例 1

以下示例演示了針對具有 https 協議的有效 url 使用 Java URLConnection setAllowUserInteraction() 方法。在此示例中,我們正在建立 URL 類的例項。使用 url.openConnection() 方法,我們獲得了 URLConnection 例項。使用 getAllowUserInteraction(),我們獲取 URLConnection 例項的 allowUserInteraction 欄位的狀態並列印它。現在使用 setAllowUserInteraction() 方法,我們將 allowUserInteraction 欄位的值設定為 true,然後使用 getAllowUserInteraction(),我們獲取 URLConnection 例項的 allowUserInteraction 欄位的狀態並列印它。

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://tutorialspoint.tw");
         URLConnection urlConnection = url.openConnection();

         System.out.println(urlConnection.getAllowUserInteraction());
         urlConnection.setAllowUserInteraction(true);
         System.out.println(urlConnection.getAllowUserInteraction());
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

輸出

false
true

示例 2

以下示例演示了針對具有 https 協議的有效 url 使用 Java URLConnection setAllowUserInteraction() 方法。在此示例中,我們正在建立 URL 類的例項。使用 url.openConnection() 方法,我們獲得了 URLConnection 例項。現在使用 setAllowUserInteraction() 方法,我們將 allowUserInteraction 欄位的值設定為 true,然後使用 getAllowUserInteraction(),我們獲取 URLConnection 例項的 allowUserInteraction 欄位的狀態並列印它。

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://tutorialspoint.tw");
         URLConnection urlConnection = url.openConnection();
         urlConnection.setAllowUserInteraction(true);
         System.out.println(urlConnection.getAllowUserInteraction());

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

輸出

true

示例 3

以下示例演示了針對具有 https 協議的有效 url 使用 Java URLConnection setAllowUserInteraction() 方法。在此示例中,我們正在建立 URL 類的例項。使用 url.openConnection() 方法,我們獲得了 URLConnection 例項。現在使用 setAllowUserInteraction() 方法,我們將 allowUserInteraction 欄位的值設定為 false,然後使用 getAllowUserInteraction(),我們獲取 URLConnection 例項的 allowUserInteraction 欄位的狀態並列印它。

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://tutorialspoint.tw");
         URLConnection urlConnection = url.openConnection();
         urlConnection.setAllowUserInteraction(false);
         System.out.println(urlConnection.getAllowUserInteraction());

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

輸出

false
java_urlconnection.htm
廣告