问题
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