Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

HttpWebResponse.Headers Property

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

Namespace:  System.Net
Assemblies:   System (in System.dll)
  System.Net.Requests (in System.Net.Requests.dll)

public override WebHeaderCollection Headers { get; }

Property Value

Type: System.Net.WebHeaderCollection
A WebHeaderCollection that contains the header information returned with the response.

ExceptionCondition
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(); 

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8
Show:
© 2015 Microsoft