C#, Microsoft Windows

Get HTTP headers with C#

If you would like to get HTTP headers in C#, you can do it with a few lines of code!

1
2
3
4
5
6
7
8
9
String url = "http://www.site.com";
 
WebRequest webrequest = HttpWebRequest.Create(url);
WebResponse webresponse = webrequest.GetResponse();
 
foreach (String key in webresponse.Headers)
    Console.WriteLine("{0}: {1}", key, webresponse.Headers[key]);
 
webresponse.Close();