C#获取接收Http Header信息

C#通过url获取Http Header的详细信息

存储在Dictionary数据字典里面

/// <summary>
/// 返回 HTTP Headers 
/// </summary>
/// <param name="Url">url</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;
}


//用法:
// Retrieve headers:
Dictionary<string, string> Headers = GetHTTPResponseHeaders("http://www.yunjson.com");
 
// And output them:
foreach (string HeaderKey in Headers.Keys) 
    Console.WriteLine("{0}: {1}", HeaderKey, Headers[HeaderKey]);


原文链接:C#获取接收Http Header信息