Export (0) Print
Expand All

HttpListenerResponse.Headers Property

Gets or sets the collection of header name/value pairs returned by the server.

Namespace: System.Net
Assembly: System (in system.dll)

public:
property WebHeaderCollection^ Headers {
	WebHeaderCollection^ get ();
	void set (WebHeaderCollection^ value);
}
/** @property */
public WebHeaderCollection get_Headers ()

/** @property */
public void set_Headers (WebHeaderCollection value)

public function get Headers () : WebHeaderCollection

public function set Headers (value : WebHeaderCollection)

Not applicable.

Property Value

A WebHeaderCollection instance that contains all the explicitly set HTTP headers to be included in the response.

Exception typeCondition

InvalidOperationException

The WebHeaderCollection instance specified for a set operation is not valid for a response.

Response headers contain metadata information such as the date and time of the response, the identity of the responding server, and the MIME type of the data contained in the response body.

For a complete list of response headers, see the HttpResponseHeader enumeration.

NoteNote:

If you attempt to set a Content-Length, Keep-Alive, Transfer-Encoding, or WWW-Authenticate header using the Headers property, an exception will be thrown. Use the KeepAlive or ContentLength64 properties to set these headers. You cannot set the Transfer-Encoding or WWW-Authenticate headers manually.

The following code example demonstrates displaying the headers in a 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 static void DisplayWebHeaderCollection(HttpListenerResponse response)
{
    WebHeaderCollection headers = response.get_Headers();
    // Get each header and display each value.
    for (int iCtr1 = 0; iCtr1 < headers.get_AllKeys().get_Length(); iCtr1++) {
        String key = headers.get_AllKeys()[iCtr1];
        String values[] = headers.GetValues(key);
        if (values.get_Length() > 0) {
            Console.WriteLine("The values of the {0} header are: ", key);
            for (int iCtr2 = 0; iCtr2 < values.get_Length(); iCtr2++) {
                String value = values[iCtr2];
                Console.WriteLine("   {0}", value);
            }
        }
        else {
            Console.WriteLine(
                "There is no value associated with the header.");
        }
    }
} //DisplayWebHeaderCollection

Windows 98, Windows Server 2003, Windows XP Media Center Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft