HttpListenerRequest.Headers 属性

定义

获取在请求中发送的标头名称/值对的集合。

public:
 property System::Collections::Specialized::NameValueCollection ^ Headers { System::Collections::Specialized::NameValueCollection ^ get(); };
public System.Collections.Specialized.NameValueCollection Headers { get; }
member this.Headers : System.Collections.Specialized.NameValueCollection
Public ReadOnly Property Headers As NameValueCollection

属性值

WebHeaderCollection 包含的 HTTP 标头包括在请求中。

示例

下面的代码示例显示给定 WebHeaderCollection 对象中的所有信息。

    // Displays the header information that accompanied a request.
public static void DisplayWebHeaderCollection(HttpListenerRequest request)
{
    System.Collections.Specialized.NameValueCollection headers = request.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.");
        }
    }
}
Public Shared Sub DisplayWebHeaderCollection(ByVal request As HttpListenerRequest)
    Dim headers As System.Collections.Specialized.NameValueCollection = request.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

注解

请求标头包含元数据信息。 例如,标头可以包含将客户端引用到服务器的资源的统一资源标识符 (URI) 、客户端使用的用户代理的标识,以及响应正文中数据的可接受的 MIME 类型。

有关请求标头的完整列表,请参阅 HttpRequestHeader 枚举。

适用于

另请参阅