如何获取 HttpResponseHeader 的名称?

发布于 2024-12-10 02:57:22 字数 707 浏览 1 评论 0 原文

我可以循环遍历 response.Headers 集合并显示每个标头的值,如下所示。

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(postDest);
//Set up the request with needed properties etc.
HttpWebResponse response = req.GetResponse() as HttpWebResponse;    
for(int i= 0; i < response.Headers.Count; i++)
{
      MessageBox.Show(response.Headers[i].ToString());
}

但是我怎样才能获得每个 ResponseHeader 的名称 Field-Name 呢?

更新:

如果我这样做,我就可以获得字段名称和值。

    for (int i = 0; i < response.Headers.Count; i++)
    {
        MessageBox.Show("Field-Name is: " + response.Headers.GetKey(i).ToString() + " Value is: " + response.Headers[i].ToString());
    }

I am able to loop through a response.Headers collection and display the value for each header like this.

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(postDest);
//Set up the request with needed properties etc.
HttpWebResponse response = req.GetResponse() as HttpWebResponse;    
for(int i= 0; i < response.Headers.Count; i++)
{
      MessageBox.Show(response.Headers[i].ToString());
}

But how can I get the name Field-Name for each ResponseHeader?

Update:

If I do this I am able to get the Field-Name and the Value.

    for (int i = 0; i < response.Headers.Count; i++)
    {
        MessageBox.Show("Field-Name is: " + response.Headers.GetKey(i).ToString() + " Value is: " + response.Headers[i].ToString());
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

十秒萌定你 2024-12-17 02:57:22

为了完整起见,我应该指出,您可以只在 WebHeaderCollection 对象本身上调用 .ToString() 。 (Response.Headers.ToString())。现在我真的不推荐这个,因为官方 MSDN 评论 说道:

此 API 支持 .NET Framework 基础结构,但不支持
旨在直接从您的代码中使用。

然而,通过 ILSpy 查看代码,它并没有做任何疯狂的事情,只是根据标头在 StringBuilder 中构建名称/值行列表。它确实排除了任何未命名的标头,但我认为这些标头非常罕见。有趣的是,他们没有麻烦地在其上添加一个过时的编译器属性(也许是为了通过他们的单元测试?))所以它可能可以安全使用。但如果您担心这一点,我会推荐问题中发布的解决方案,该解决方案使用直接索引。请注意,直接索引比接受的答案中的方法更快,因为它不依赖于字符串键或内部哈希表(并不是说性能可能是该领域的主要问题。)

For completeness, I should point out that you can just call .ToString() on the WebHeaderCollection object itself. (Response.Headers.ToString()). Now I don't really recommend this, since the official MSDN comment on the method says:

This API supports the .NET Framework infrastructure and is not
intended to be used directly from your code.

However, looking at the code through ILSpy, it's not doing anything wild, just building up a list of name/value lines in a StringBuilder based on the headers. It does exclude any unnamed headers, but I think those are quite rare. And it's interesting that they didn't go to the trouble of slapping an Obsolete compiler attribute on it (maybe to pass their unit tests?)) So it's probably safe to use. But if you are worried about this, I would recommend the solution posted in the question, which uses direct indexing. Note that direct indexing is faster than the method in the accepted answer, since it doesn't rely on a String key or internal Hashtable (not that performance is likely to be a major concern in this area.)

回忆那么伤 2024-12-17 02:57:22

您可以使用response.Headers.GetValues(response.Headers.GetKey(i))

you can use response.Headers.GetValues(response.Headers.GetKey(i))

为人所爱 2024-12-17 02:57:22
foreach (HttpWebResponse v in response.Headers)
{
   v.Headers.Keys.ToString();      
}
foreach (HttpWebResponse v in response.Headers)
{
   v.Headers.Keys.ToString();      
}
暖心男生 2024-12-17 02:57:22

你可以这样做:

foreach (var key in response.Headers.AllKeys)
{
  Console.WriteLine(response.Headers[key]);
}

You can just do:

foreach (var key in response.Headers.AllKeys)
{
  Console.WriteLine(response.Headers[key]);
}
罪#恶を代价 2024-12-17 02:57:22
var values = response.Headers.GetValues("Key")

或者

 var values = Enumerable.Empty<string>();
 if (response.Headers.TryGetValues("Key", out values))
 {
     // check values
 }
var values = response.Headers.GetValues("Key")

or

 var values = Enumerable.Empty<string>();
 if (response.Headers.TryGetValues("Key", out values))
 {
     // check values
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文