WebResponse.Headers 屬性

定義

在衍生類別中覆寫時,取得與這個要求相關聯的標頭名稱值配對集合。

public:
 virtual property System::Net::WebHeaderCollection ^ Headers { System::Net::WebHeaderCollection ^ get(); };
public virtual System.Net.WebHeaderCollection Headers { get; }
member this.Headers : System.Net.WebHeaderCollection
Public Overridable ReadOnly Property Headers As WebHeaderCollection

屬性值

WebHeaderCollection 類別的執行個體,包含與這個回應相關聯的標頭值。

例外狀況

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

範例

下列範例會顯示 中 WebResponse傳回的所有標頭名稱/值組。

// Create a 'WebRequest' object with the specified url.
WebRequest^ myWebRequest = WebRequest::Create( "http://www.contoso.com" );

// Send the 'WebRequest' and wait for response.
WebResponse^ myWebResponse = myWebRequest->GetResponse();

// Display all the Headers present in the response received from the URl.
Console::WriteLine( "\nThe following headers were received in the response" );

// Display each header and its key , associated with the response object.
for ( int i = 0; i < myWebResponse->Headers->Count; ++i )
   Console::WriteLine( "\nHeader Name: {0}, Header value : {1}", myWebResponse->Headers->Keys[ i ], myWebResponse->Headers[ i ] );

// Release resources of response object.
myWebResponse->Close();

    // Create a 'WebRequest' object with the specified url. 	
   WebRequest myWebRequest = WebRequest.Create("http://www.contoso.com"); 

   // Send the 'WebRequest' and wait for response.
WebResponse myWebResponse = myWebRequest.GetResponse(); 

   // Display all the Headers present in the response received from the URl.
   Console.WriteLine("\nThe following headers were received in the response");

// Display each header and it's key , associated with the response object.
   for(int i=0; i < myWebResponse.Headers.Count; ++i)  
      Console.WriteLine("\nHeader Name:{0}, Header value :{1}",myWebResponse.Headers.Keys[i],myWebResponse.Headers[i]); 

   // Release resources of response object.
   myWebResponse.Close();

    ' Create a 'WebRequest' object with the specified url 	
    Dim myWebRequest As WebRequest = WebRequest.Create("www.contoso.com")
    
    ' Send the 'WebRequest' and wait for response.
    Dim myWebResponse As WebResponse = myWebRequest.GetResponse()
    
    ' Display all the Headers present in the response received from the URl.
    Console.WriteLine(ControlChars.Cr + "The following headers were received in the response")
    
    ' Headers property is a 'WebHeaderCollection'. Use it's properties to traverse the collection and display each header
    Dim i As Integer
    
    While i < myWebResponse.Headers.Count
        Console.WriteLine(ControlChars.Cr + "Header Name:{0}, Header value :{1}", myWebResponse.Headers.Keys(i), myWebResponse.Headers(i))
i = i + 1
    End While

    ' Release resources of response object.
    myWebResponse.Close()

備註

屬性 Headers 包含響應中傳回的名稱/值標頭組。

注意

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

適用於

另請參閱