Java 教程

Java 控制語句

面向物件程式設計

Java 內建類

Java 檔案處理

Java 錯誤與異常

Java 多執行緒

Java 同步

Java 網路程式設計

Java 集合

Java 介面

Java 資料結構

Java 集合演算法

高階 Java

Java 雜項

Java API 與框架

Java 類引用

Java 有用資源

Java - URLConnection getRequestProperties() 方法



描述

Java URLConnection getRequestProperties() 方法返回此連線的常規請求屬性的不可修改 Map。Map 的鍵是表示請求頭欄位名稱的字串。每個 Map 值都是一個不可修改的字串列表,表示相應的欄位值。

宣告

以下是 java.net.URLConnection.getRequestProperties() 方法的宣告

public Map<String,List<String>> getRequestProperties()

引數

返回值

此連線的常規請求屬性的 Map。

異常

IllegalStateException - 如果已連線。

示例 1

以下示例演示了使用 Java URLConnection getRequestProperties() 方法處理具有 https 協議的有效 url。在此示例中,我們正在建立 URL 類的例項。使用 url.openConnection() 方法,我們獲得了 URLConnection 例項。使用 addRequestProperty(),我們添加了一個請求屬性,然後使用 getRequestProperties() 方法檢查並列印它。

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.addRequestProperty("content-type", "txt");
         System.out.println(urlConnection.getRequestProperties());

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

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

輸出

{content-type=[txt]}

示例 2

以下示例演示了使用 Java URLConnection addRequestProperty() 方法處理具有 https 協議的有效 url。在此示例中,我們正在建立 URL 類的例項。使用 url.openConnection() 方法,我們獲得了 URLConnection 例項。使用 addRequestProperty(),我們添加了多個請求屬性,然後使用 getRequestProperties() 方法檢查並列印它們。

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.addRequestProperty("content-type", "txt");
         urlConnection.addRequestProperty("auth", "basic");
         System.out.println(urlConnection.getRequestProperties());

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

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

輸出

{auth=[basic], content-type=[txt]}

示例 3

以下示例演示了使用 Java URLConnection addRequestProperty() 方法處理具有 https 協議的有效 url。在此示例中,我們正在建立 URL 類的例項。使用 url.openConnection() 方法,我們獲得了 URLConnection 例項。使用 addRequestProperty(),我們添加了多個請求屬性,同時重複了一個鍵,然後使用 getRequestProperties() 方法檢查並列印它們。

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.addRequestProperty("content-type", "txt");
         urlConnection.addRequestProperty("auth", "basic");
         urlConnection.addRequestProperty("content-type", "json");
         System.out.println(urlConnection.getRequestProperties());

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

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

輸出

{auth=[basic], content-type=[json, txt]}

在這裡,我們可以看到請求屬性被新增到現有的鍵中,而 getRequestProperty() 返回新增到鍵的最新值。

java_urlconnection.htm
廣告