删除HTTP Web请求中的特定标头项 [英] Removing a particular header item in HTTP web requests
问题描述
我目前正在使用此方法从特定网站检索标头:
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);
}
据说,当我尝试从一个请求标题时 https
网站,在标题
部分下,它还会显示位置
,这是我们网站的URL。我想从C#代码的标题
部分删除位置
。
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.
我希望打印的标题显示 但 位置:https://www.something.com
I would like the printed headers to display everything BUT the Location: https://www.something.com
我试图像处理网络回复一样硬编码 webResponse.Headers.XXX
,但是,无效。
I tried to hardcode the webResponse.Headers.XXX
myself like I have with the web responses, however, no avail.
推荐答案
您可以使用 webResponse.Headers.Keys $阅读所有标题c $ c>。
foreach (string key in webResponse.Headers.Keys)
{
if (key != "Location")
{
var value = webResponse.Headers[key];
headers.Add(key, value);
}
}
这篇关于删除HTTP Web请求中的特定标头项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!