In this post I will show how to fetch header info from given url
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; namespace SNIPPET_CS { public class HeaderInfo { public string Key { get; set; } public string Value { get; set; } public static List<HeaderInfo> GetHttpResponseHeaders(string url) { List<HeaderInfo> headerList = new List<HeaderInfo>(); WebRequest webRequestObject = WebRequest.Create(url); WebResponse responseObject = webRequestObject.GetResponse(); if (responseObject != null) { headerList.AddRange(from string headerKey in responseObject.Headers select new HeaderInfo() { Key = headerKey, Value = responseObject.Headers[headerKey] }); responseObject.Close(); } return headerList; } } class Program { static void Main(string[] args) { // Retrieve headers: var headers = HeaderInfo.GetHttpResponseHeaders("http://www.google.com"); // And output them: foreach (HeaderInfo headerKey in headers) Console.WriteLine("{0}: {1}", headerKey.Key, headerKey.Value); } } }



No comments:
Post a Comment