.net 如何遍历 WebHeaderCollection
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6439225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to loop through WebHeaderCollection
提问by gusaindpk
How do you loop through a WebHeaderCollection got from HttpWebResponse in windows phone 7 to get keys and values? We've tried Enumerator.Current; with this, we are only getting the keys, not the values. We are doing this to get a redirected URL.
您如何遍历从 Windows Phone 7 中的 HttpWebResponse 获得的 WebHeaderCollection 以获取键和值?我们已经尝试过 Enumerator.Current; 有了这个,我们只得到键,而不是值。我们这样做是为了获取重定向的 URL。
回答by ony
That's an awful collection, I think.
那是一个糟糕的收藏,我想。
See MSDN sample. I'd prefer this one:
请参阅MSDN 示例。我更喜欢这个:
var headers = new System.Net.WebHeaderCollection();
headers.Add("xxx", "yyy");
headers.Add("zzz", "fff");
headers.Add("xxx", "ttt");
for(int i = 0; i < headers.Count; ++i)
{
string header = headers.GetKey(i);
foreach(string value in headers.GetValues(i))
{
Console.WriteLine("{0}: {1}", header, value);
}
}
Unfortunately there is no way to get values with order preserving between other headers.
不幸的是,没有办法在其他标头之间保留顺序的值。
P.S. Linq style (in LINQPad)
PS Linq 样式(在LINQPad 中)
var items = Enumerable
.Range(0, headers.Count)
.SelectMany(i => headers.GetValues(i)
.Select(v => Tuple.Create(headers.GetKey(i), v))
);
items.Dump();
回答by Igor Be
foreach(string key in resp.AllKeys)
{
string value = resp[key];
}
回答by Rohith Nandakumar
in WP7/Silverlight I do this.
在 WP7/Silverlight 我这样做。
foreach (string key in headers.AllKeys)
{
Console.WriteLine("Header : " + key + " ---> " + headers[key]);
}
回答by C?ur
In Silverlight.
在银光。
If you want the keys and values one by one:
如果你想要一个一个的键和值:
foreach (string key in webHeaderCollection)
{
var value = webHeaderCollection[key];
// do something with key and value
}
If you want a dictionary of keys and values:
如果你想要一个键和值的字典:
var dic = webHeaderCollection.AllKeys.ToDictionary(k => webHeaderCollection[k]);
foreach (var pair in MyDic)
{
// do something with pair.Key and pair.Value
}
回答by Ben Novoselsky
How about some C# 6 :)
一些 C# 6 怎么样:)
string.Join("&", headers.AllKeys.Select(key => $"{key}={headers[key]}").ToList());
回答by Nicholas Petersen
I really don't like it when special collections like this exist at times in .NET that are not easily iterated over for expected values. Makes things a lot less user friendly. Anyway, if you felt like going to the trouble of adding an extension method:
当 .NET 中有时存在这样的特殊集合时,我真的不喜欢它,这些集合不容易迭代以获得预期值。使事情变得不那么用户友好。无论如何,如果您想麻烦添加扩展方法:
// call in your main code
KeyValuePair<string, string>[] headers = webResponse.Headers.GetHeaders();
// extension:
public static class Xtension
{
public static KeyValuePair<string, string>[] GetHeaders(this WebHeaderCollection webHeaderCollection)
{
string[] keys = webHeaderCollection.AllKeys;
var keyVals = new KeyValuePair<string, string>[keys.Length];
for (int i = 0; i < keys.Length; i++)
keyVals[i] = new KeyValuePair<string, string>(keys[i], webHeaderCollection[keys[i]]);
return keyVals;
}
}
回答by Jon Schneider
I landed on this question from Google while I was trying to find a way to view the key-value pairs in a WebHeaderCollection from the Visual Studio debugger.
当我试图找到一种方法来查看来自 Visual Studio 调试器的 WebHeaderCollection 中的键值对时,我从 Google 找到了这个问题。
Simple solution (in retrospect): The WebHeaderCollection.ToString() method, used in the Watch debugging window, will accomplish this:
简单的解决方案(回想起来):在 Watch 调试窗口中使用的 WebHeaderCollection.ToString() 方法将完成此操作:
webheadercollection.ToString()
So, if you have an HttpWebRequest named request:
所以,如果你有一个名为 HttpWebRequest 的request:
request.Headers.ToString()
回答by Abacus
My solution, as an extension method:
我的解决方案,作为扩展方法:
private static string Serialize(this System.Net.WebHeaderCollection value)
{
var response = new System.Text.StringBuilder();
foreach (string k in value.Keys)
response.AppendLine(k + ": " + value[k]);
return response.ToString();
}
回答by Justin Helgerson
For i As Integer = 0 To Response.Headers.Count - 1
'Response.Headers.Get(i)
Next
回答by u4069409
If you really want to use extension method to achieve getting list of headers as KeyValuePair array you may want to use IEnumerable interface.
如果您真的想使用扩展方法来实现获取标头列表作为 KeyValuePair 数组,您可能需要使用 IEnumerable 接口。
public static class WebHeaderCollectionExtensions
{
public static IEnumerable<KeyValuePair<string, string>> GetHeaders(this System.Net.WebHeaderCollection webHeaderCollection)
{
string[] keys = webHeaderCollection.AllKeys;
for (int i = 0; i < keys.Length; i++)
{
yield return new KeyValuePair<string, string>(keys[i], webHeaderCollection[keys[i]]);
}
}
}
In this case you can iterate in foreach loop easy way:
在这种情况下,您可以轻松地在 foreach 循环中进行迭代:
foreach (var x in h.GetHeaders())
{
Console.WriteLine(x.Key + ": " + x.Value);
}

