HttpListenerResponse.Headers HttpListenerResponse.Headers HttpListenerResponse.Headers HttpListenerResponse.Headers Property

定義

取得或設定伺服器所傳回之標頭名稱/值組的集合。Gets or sets the collection of header name/value pairs returned by the server.

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

屬性值

WebHeaderCollection 執行個體,包含回應中所要包含的所有明確設定 HTTP 標頭。A WebHeaderCollection instance that contains all the explicitly set HTTP headers to be included in the response.

例外狀況

為設定作業指定的 WebHeaderCollection 執行個體,對回應無效。The WebHeaderCollection instance specified for a set operation is not valid for a response.

範例

下列程式碼範例會示範如何顯示中的標頭WebHeaderCollectionThe following code example demonstrates displaying the headers in a WebHeaderCollection.

    // Displays the header information that accompanied a request.
public static void DisplayWebHeaderCollection(HttpListenerResponse response)
{
    WebHeaderCollection headers = response.Headers;
    // Get each header and display each value.
    foreach (string key in headers.AllKeys)
    {
        string[] values = headers.GetValues(key);
        if(values.Length > 0) 
        {
            Console.WriteLine("The values of the {0} header are: ", key);
            foreach (string value in values) 
            {
                Console.WriteLine("   {0}", value);
            }
        }
        else
            Console.WriteLine("There is no value associated with the header.");
    }
}
' Displays the header information that accompanied a request.
Public Shared Sub DisplayWebHeaderCollection(ByVal response As HttpListenerResponse)
    Dim headers As WebHeaderCollection = response.Headers

    ' Get each header and display each value.
    For Each key As String In headers.AllKeys
        Dim values As String() = headers.GetValues(key)

        If values.Length > 0 Then
            Console.WriteLine("The values of the {0} header are: ", key)

            For Each value As String In values
                Console.WriteLine("   {0}", value)
            Next
        Else
            Console.WriteLine("There is no value associated with the header.")
        End If
    Next
End Sub

備註

回應標頭包含中繼資料資訊,例如日期和時間的回應,回應的伺服器,並在回應主體中包含的資料的 MIME 類型的身分識別。Response headers contain metadata information such as the date and time of the response, the identity of the responding server, and the MIME type of the data contained in the response body.

如需回應標頭的完整清單,請參閱HttpResponseHeader列舉型別。For a complete list of response headers, see the HttpResponseHeader enumeration.

注意

如果您嘗試設定內容長度保持連線、 傳輸編碼,或使用的標頭屬性時,發生例外狀況的 WWW 驗證標頭將會擲回。If you attempt to set a Content-Length, Keep-Alive, Transfer-Encoding, or WWW-Authenticate header using the Headers property, an exception will be thrown. 使用KeepAliveContentLength64屬性來設定這些標頭。Use the KeepAlive or ContentLength64 properties to set these headers. 您無法手動設定的傳輸編碼,或 Www-authenticate 標頭。You cannot set the Transfer-Encoding or WWW-Authenticate headers manually.

適用於

另請參閱