Export (0) Print
Expand All

HttpWebResponse.Headers Property

Gets the headers that are associated with this response from the server.

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

public override WebHeaderCollection Headers { get; }
/** @property */
public WebHeaderCollection get_Headers ()

public override function get Headers () : WebHeaderCollection

Not applicable.

Property Value

A WebHeaderCollection that contains the header information returned with the response.

Exception typeCondition

ObjectDisposedException

The current instance has been disposed.

The Headers property is a collection of name/value pairs that contain the HTTP header values returned with the response. Common header information returned from the Internet resource is exposed as properties of the HttpWebResponse class. The following table lists common headers that the API exposes as properties.

Header

Property

Content-Encoding

ContentEncoding

Content-Length

ContentLength

Content-Type

ContentType

Last-Modified

LastModified

Server

Server

The following example writes the contents of all of the response headers to the console.

// Creates an HttpWebRequest for the specified URL. 
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); 
// Sends the HttpWebRequest and waits for response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 
                        
// Displays all the headers present in the response received from the URI.
Console.WriteLine("\r\nThe following headers were received in the response:");
// Displays each header and it's key associated with the response.
for(int i=0; i < myHttpWebResponse.Headers.Count; ++i)  
    Console.WriteLine("\nHeader Name:{0}, Value :{1}",myHttpWebResponse.Headers.Keys[i],myHttpWebResponse.Headers[i]); 
// Releases the resources of the response.
myHttpWebResponse.Close(); 

// Creates an HttpWebRequest for the specified URL. 
HttpWebRequest myHttpWebRequest = (HttpWebRequest)
    WebRequest.Create(url);
// Sends the HttpWebRequest and waits for response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)
    myHttpWebRequest.GetResponse();
// Displays all the headers present in the response 
// received from the URI.
Console.WriteLine("\r\nThe following headers were received"
    + " in the response:");
// Displays each header and it's key associated with the response.
for (int i = 0; i < myHttpWebResponse.get_Headers().get_Count();
    ++i) {
    Console.WriteLine("\nHeader Name:{0}, Value :{1}", 
        myHttpWebResponse.get_Headers().get_Keys().get_Item(i), 
        myHttpWebResponse.get_Headers().get_Item(i));
}  // Releases the resources of the response.

myHttpWebResponse.Close();

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 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, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft