HttpListenerResponse.Headers 屬性

定義

取得或設定伺服器所傳回之標頭名稱/值組的集合。

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 標頭。

例外狀況

為設定作業指定的 WebHeaderCollection 執行個體,對回應無效。

範例

下列程式碼範例示範如何在 中 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 類型。

如需回應標頭的完整清單,請參閱 HttpResponseHeader 列舉。

注意

如果您嘗試使用 Headers 屬性設定 Content-Length、Keep-Alive、Transfer-Encoding 或WWW-Authenticate標頭,將會擲回例外狀況。 KeepAlive使用 或 ContentLength64 屬性來設定這些標頭。 您無法手動設定Transfer-Encoding或WWW-Authenticate標頭。

適用於

另請參閱