Java 教程

Java 控制語句

面向物件程式設計

Java 內建類

Java 檔案處理

Java 錯誤和異常

Java 多執行緒

Java 同步

Java 網路

Java 集合

Java 介面

Java 資料結構

Java 集合演算法

高階 Java

Java 雜項

Java API 和框架

Java 類參考

Java 有用資源

Java - URLConnection getRequestProperty(String key)



描述

Java URLConnection getRequestProperty(String key) 方法返回此連線的命名通用請求屬性的值。

宣告

以下是 java.net.URLConnection.getRequestProperty(String key) 方法的宣告

public String getRequestProperty(String key)

引數

key − 請求所依據的關鍵字。

返回值

此連線的命名通用請求屬性的值。如果 key 為 null,則返回 null。

異常

IllegalStateException − 如果已連線。

示例 1

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

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.getRequestProperty("content-type"));

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

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

輸出

txt

示例 2

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

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.getRequestProperty("content-type"));

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

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

輸出

txt

示例 3

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

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.getRequestProperty("content-type"));

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

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

輸出

json

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

java_urlconnection.htm
廣告