Get HTTP header

Submitted by:Andery Smith

Date added:05 August, 2015

Category:C#

Shows how to access the HTTP headers from a given web adress.

Tags: http headers , access header

Code Snippet:

/// <summary>
/// Returns the responded HTTP headers of the given URL.
/// </summary>
/// <param name="Url">The adress.</param>
/// <returns>List of headers</returns>
public Dictionary<string, string> GetHTTPResponseHeaders(string Url)
{
Dictionary<string, string> HeaderList = new Dictionary<string, string>();

WebRequest WebRequestObject = HttpWebRequest.Create(Url);
WebResponse ResponseObject = WebRequestObject.GetResponse();

foreach (string HeaderKey in ResponseObject.Headers)
HeaderList.Add(HeaderKey, ResponseObject.Headers[HeaderKey]);

ResponseObject.Close();

return HeaderList;
}
 
 

Comments