删除HTTP Web请求中的特定标头项 [英] Removing a particular header item in HTTP web requests

查看:135
本文介绍了删除HTTP Web请求中的特定标头项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用此方法从特定网站检索标头:

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

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

这篇关于删除HTTP Web请求中的特定标头项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆