Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

HttpWebResponse.Headers Property

Gets the headers associated with this response from the server.

[Visual Basic]
Overrides Public ReadOnly Property Headers As WebHeaderCollection
[C#]
public override WebHeaderCollection Headers {get;}
[C++]
public: __property WebHeaderCollection* get_Headers();
[JScript]
public override function get Headers() : WebHeaderCollection;

Property Value

A WebHeaderCollection containing the header information returned with the response.

Exceptions

Exception Type Condition
ObjectDisposedException The current instance has been disposed.

Remarks

The Headers property is a collection of name/value pairs containing 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

Example

[Visual Basic, C#, C++] The following example writes the contents of all of the response headers to the console.

[Visual Basic] 
' Creates an HttpWebRequest with the specified URL. 
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
' Sends the HttpWebRequest and waits for a response.
Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
' Displays all the Headers present in the response received from the URI.
Console.WriteLine(ControlChars.Lf + ControlChars.Cr + "The following headers were received in the response")
'The Headers property is a WebHeaderCollection. Use it's properties to traverse the collection and display each header.
Dim i As Integer
While i < myHttpWebResponse.Headers.Count
    Console.WriteLine(ControlChars.Cr + "Header Name:{0}, Value :{1}", myHttpWebResponse.Headers.Keys(i), myHttpWebResponse.Headers(i))
  i = i + 1
End While
myHttpWebResponse.Close()

[C#] 
// 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(); 

[C++] 
// Creates an HttpWebRequest for the specified URL.
HttpWebRequest* myHttpWebRequest =
   dynamic_cast<HttpWebRequest*>(WebRequest::Create(url));
// Sends the HttpWebRequest and waits for response.
HttpWebResponse* myHttpWebResponse =
   dynamic_cast<HttpWebResponse*>(myHttpWebRequest->GetResponse());

// Displays all the headers present in the response received from the URI.
Console::WriteLine(S"\r\nThe following headers were received in the response:");
// Displays each header and its key associated with the response.
for (int i=0; i < myHttpWebResponse->Headers->Count; ++i)
   Console::WriteLine(S"\nHeader Name: {0}, Value : {1}",
   myHttpWebResponse->Headers->Keys->Item[i], myHttpWebResponse->Headers->Item[i]);
// Releases the resources of the response.
myHttpWebResponse->Close();

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

HttpWebResponse Class | HttpWebResponse Members | System.Net Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.