Removing a particular header item in HTTP web requests

南楼画角 提交于 2019-12-25 16:54:15

问题


I am currently using this method to retrieve headers from a particular site:

List<string> headers = new List<string>();
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.CookieContainer = new CookieContainer();
webRequest.AllowAutoRedirect = false;

using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
    // headers.Add("URL: " + url);
    headers.Add("Status Code: " + (int)webResponse.StatusCode);
    headers.Add("Status Desc: " + webResponse.StatusDescription);
    headers.Add("Headers: " + webResponse.Headers);
}

With that said, it appears that when I try to request the headers from a https site, under the header section, it also displays the Location, which is the URL to our site. I would like to remove the Location from the Headers section of the C# code.

I would like the printed headers to display everything BUT the Location: https://www.something.com

I tried to hardcode the webResponse.Headers.XXX myself like I have with the web responses, however, no avail.


回答1:


You can read all the headers by using webResponse.Headers.Keys.

foreach (string key in webResponse.Headers.Keys)
{
    if (key != "Location")
    {
        var value = webResponse.Headers[key];
        headers.Add(key, value);
    }
}


来源:https://stackoverflow.com/questions/21788722/removing-a-particular-header-item-in-http-web-requests

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!