HttpWebResponse.Headers Property
Gets the headers that are associated with this response from the server.
Namespace: System.Net
Assembly: System (in System.dll)
Property Value
Type: System.Net.WebHeaderCollectionA WebHeaderCollection that contains the header information returned with the response.
| Exception | Condition |
|---|---|
| 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 | |
Content-Length | |
Content-Type | |
Last-Modified | |
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();
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.