Skip to main content
.NET Framework Class Library
HttpListenerRequest..::.Headers Property

Gets the collection of header name/value pairs sent in the request.

Namespace: System.Net
Assembly: System (in System.dll)
Syntax
Public ReadOnly Property Headers As NameValueCollection
public NameValueCollection Headers { get; }
public:
property NameValueCollection^ Headers {
	NameValueCollection^ get ();
}
member Headers : NameValueCollection

Property Value

Type: System.Collections.Specialized..::.NameValueCollection
A WebHeaderCollection that contains the HTTP headers included in the request.
Remarks

Request headers contain metadata information. For example, headers can contain the Uniform Resource Identifier (URI) of the resource that referred the client to the server, the identity of the user agent employed by the client, and the acceptable MIME types for data in the response body.

For a complete list of request headers, see the HttpRequestHeader enumeration.

Examples

The following code example displays all the information in a given WebHeaderCollection object.


' Displays the header information that accompanied a request.
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 value
        Else
            Console.WriteLine("There is no value associated with the header.")
        End If
    Next key
End Sub


    // 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.");
    }
}

Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.