HttpListenerResponse.Headers Vlastnost

Definice

Získá nebo nastaví kolekci hlaviček párů název/hodnota vrácená serverem.

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

Hodnota vlastnosti

Instance WebHeaderCollection , která obsahuje všechny explicitně nastavené hlavičky HTTP, které mají být zahrnuty do odpovědi.

Výjimky

Instance WebHeaderCollection zadaná pro operaci set není platná pro odpověď.

Příklady

Následující příklad kódu ukazuje zobrazení hlaviček v .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

Poznámky

Hlavičky odpovědi obsahují informace metadat, jako je datum a čas odpovědi, identita serveru, který odpovídá, a typ MIME dat obsažených v textu odpovědi.

Úplný seznam hlaviček odpovědi najdete ve výčtu HttpResponseHeader .

Poznámka

Pokud se pokusíte nastavit hlavičku Content-Length, Keep-Alive, Transfer-Encoding nebo WWW-Authenticate pomocí vlastnosti Headers, vyvolá se výjimka. KeepAlive K nastavení těchto hlaviček použijte vlastnosti nebo ContentLength64 . Záhlaví Transfer-Encoding nebo WWW-Authenticate nelze nastavit ručně.

Platí pro

Viz také