HttpListenerResponse.Headers Właściwość

Definicja

Pobiera lub ustawia kolekcję par nazwa/wartość nagłówka zwróconych przez serwer.

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

Wartość właściwości

WebHeaderCollection

WebHeaderCollection Wystąpienie, które zawiera wszystkie jawnie ustawione nagłówki HTTP, które mają zostać uwzględnione w odpowiedzi.

Wyjątki

Wystąpienie WebHeaderCollection określone dla operacji zestawu jest nieprawidłowe dla odpowiedzi.

Przykłady

Poniższy przykład kodu przedstawia wyświetlanie nagłówków w obiekcie 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

Uwagi

Nagłówki odpowiedzi zawierają informacje o metadanych, takie jak data i godzina odpowiedzi, tożsamość serwera odpowiedzi i typ MIME danych zawartych w treści odpowiedzi.

Aby uzyskać pełną listę nagłówków odpowiedzi, zobacz HttpResponseHeader wyliczenie.

Uwaga

Jeśli spróbujesz ustawić nagłówek Content-Length, Keep-Alive, Transfer-Encoding lub WWW-Authenticate przy użyciu właściwości Headers, zostanie zgłoszony wyjątek. KeepAlive Użyj właściwości lubContentLength64, aby ustawić te nagłówki. Nie można ręcznie ustawić nagłówków Transfer-Encoding ani WWW-Authenticate.

Dotyczy

Zobacz też