共用方式為


WebRequest.Headers 屬性

定義

在子代類別中覆寫時,取得或設定與要求相關聯的標頭名稱/值組集合。

public:
 abstract property System::Net::WebHeaderCollection ^ Headers { System::Net::WebHeaderCollection ^ get(); void set(System::Net::WebHeaderCollection ^ value); };
public:
 virtual property System::Net::WebHeaderCollection ^ Headers { System::Net::WebHeaderCollection ^ get(); void set(System::Net::WebHeaderCollection ^ value); };
public abstract System.Net.WebHeaderCollection Headers { get; set; }
public virtual System.Net.WebHeaderCollection Headers { get; set; }
member this.Headers : System.Net.WebHeaderCollection with get, set
Public MustOverride Property Headers As WebHeaderCollection
Public Overridable Property Headers As WebHeaderCollection

屬性值

WebHeaderCollection,其中包含與此要求相關聯的標頭名稱/值組。

例外狀況

當屬性未在子系類別中覆寫時,會嘗試取得或設定 屬性。

範例

下列範例會顯示與此要求相關聯的標頭名稱/值組。

// Create a new request to the mentioned URL.
WebRequest^ myWebRequest = WebRequest::Create( "http://www.contoso.com" );

// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse^ myWebResponse = myWebRequest->GetResponse();

// Release the resources of response object.
myWebResponse->Close();
Console::WriteLine( "\nThe HttpHeaders are \n {0}", myWebRequest->Headers );

         // Create a new request to the mentioned URL.	
WebRequest myWebRequest=WebRequest.Create("http://www.contoso.com");		

// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse myWebResponse=myWebRequest.GetResponse();

// Release the resources of response object.
myWebResponse.Close();
Console.WriteLine("\nThe HttpHeaders are \n{0}",myWebRequest.Headers);

    ' Create a new request to the mentioned URL.	
    Dim myWebRequest As WebRequest = WebRequest.Create("http://www.contoso.com")
    
   ' Assign the response object of 'WebRequest' to a 'WebResponse' variable.
   
    Dim myWebResponse As WebResponse = myWebRequest.GetResponse()
   ' Release the resources of response object.
   
 myWebResponse.Close()
Console.WriteLine(ControlChars.Cr + "The HttpHeaders are " + ControlChars.Cr + "{0}", myWebRequest.Headers)

備註

謹慎

WebRequestHttpWebRequestServicePointWebClient 已經過時,您不應該將它們用於新的開發。 請改用 HttpClient

Headers 屬性包含 WebHeaderCollection 實例,其中包含要傳送至因特網資源的標頭資訊。

注意

WebRequest 類別是 abstract 類別。 運行時間 WebRequest 實例的實際行為是由 WebRequest.Create 方法所傳回的子代類別所決定。 如需預設值和例外狀況的詳細資訊,請參閱子代類別的檔,例如 HttpWebRequestFileWebRequest

適用於

另請參閱