Java 教程

Java 控制語句

面向物件程式設計

Java 內建類

Java 檔案處理

Java 錯誤和異常

Java 多執行緒

Java 同步

Java 網路

Java 集合

Java 介面

Java 資料結構

Java 集合演算法

高階 Java

Java 雜項

Java API 和框架

Java 類參考

Java 有用資源

Java - URLConnection setDefaultAllowUserInteraction(boolean defaultallowuserinteraction)



描述

Java URLConnection setDefaultAllowUserInteraction(boolean defaultallowuserinteraction) 方法將所有未來 URLConnection 物件的 allowUserInteraction 欄位的預設值設定為指定值。

宣告

以下是 java.net.URLConnection.setDefaultAllowUserInteraction(boolean defaultallowuserinteraction) 方法的宣告

public static void setDefaultAllowUserInteraction(boolean defaultallowuserinteraction)

引數

defaultallowuserinteraction − 新值。

返回值

AllowUserInteraction 欄位的預設值。

異常

示例 1

以下示例演示瞭如何使用 Java URLConnection setDefaultAllowUserInteraction() 方法處理具有 https 協議的有效 url。在此示例中,我們建立了一個 URL 類的例項。使用 url.openConnection() 方法,我們獲取了 URLConnection 例項。使用 getDefaultAllowUserInteraction(),我們獲取了 URLConnection 例項的 allowUserInteraction 欄位的預設值並打印出來。現在使用 setDefaultAllowUserInteraction(),我們將 URLConnection 例項的 allowUserInteraction 欄位的預設值設定為 true,然後打印出來。

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.getDefaultAllowUserInteraction());
         urlConnection.setDefaultAllowUserInteraction(true);
         System.out.println(urlConnection.getDefaultAllowUserInteraction());
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

輸出

false
true

示例 2

以下示例演示瞭如何使用 Java URLConnection setDefaultAllowUserInteraction() 方法處理具有 http 協議的有效 url。在此示例中,我們建立了一個 URL 類的例項。使用 url.openConnection() 方法,我們獲取了 URLConnection 例項。使用 getDefaultAllowUserInteraction(),我們獲取了 URLConnection 例項的 allowUserInteraction 欄位的預設值並打印出來。現在使用 setDefaultAllowUserInteraction(),我們將 URLConnection 例項的 allowUserInteraction 欄位的預設值設定為 true,然後打印出來。

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.getDefaultAllowUserInteraction());
         urlConnection.setDefaultAllowUserInteraction(true);
         System.out.println(urlConnection.getDefaultAllowUserInteraction());
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

輸出

false
true

示例 3

以下示例演示瞭如何使用 Java URLConnection setDefaultAllowUserInteraction() 方法處理 google 的具有 http 協議的 url。在此示例中,我們建立了一個 URL 類的例項。使用 url.openConnection() 方法,我們獲取了 URLConnection 例項。使用 getDefaultAllowUserInteraction(),我們獲取了 URLConnection 例項的 allowUserInteraction 欄位的預設值並打印出來。現在使用 setDefaultAllowUserInteraction(),我們將 URLConnection 例項的 allowUserInteraction 欄位的預設值設定為 true,然後打印出來。

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("http://www.google.com");
         URLConnection urlConnection = url.openConnection();

         System.out.println(urlConnection.getDefaultAllowUserInteraction());
         urlConnection.setDefaultAllowUserInteraction(true);
         System.out.println(urlConnection.getDefaultAllowUserInteraction());
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

輸出

false
true
java_urlconnection.htm
廣告

© . All rights reserved.