How to loop through WebHeaderCollection
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.
That's an awful collection, I think.
See MSDN sample. I'd prefer this one:
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)
var items = Enumerable
.Range(0, headers.Count)
.SelectMany(i => headers.GetValues(i)
.Select(v => Tuple.Create(headers.GetKey(i), v))
);
items.Dump();
foreach(string key in resp.AllKeys)
{
string value = resp[key];
}
in WP7/Silverlight I do this.
foreach (string key in headers.AllKeys)
{
Console.WriteLine("Header : " + key + " ---> " + headers[key]);
}
How about some C# 6 :)
string.Join("&", headers.AllKeys.Select(key => $"{key}={headers[key]}").ToList());
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
}
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:
// 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;
}
}
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.
Simple solution (in retrospect): The WebHeaderCollection.ToString() method, used in the Watch debugging window, will accomplish this:
webheadercollection.ToString()
So, if you have an HttpWebRequest named request
:
request.Headers.ToString()
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();
}
For i As Integer = 0 To Response.Headers.Count - 1
'Response.Headers.Get(i)
Next
Extension method for your library:
public static IEnumerable<KeyValuePair<string, string>> AsEnumerable(this WebHeaderCollection headers) =>
headers.AllKeys.Select(k => new KeyValuePair<string, string>(k, headers[k])).AsEnumerable();
If you really want to use extension method to achieve getting list of headers as KeyValuePair array you may want to use IEnumerable interface.
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 (var x in h.GetHeaders())
{
Console.WriteLine(x.Key + ": " + x.Value);
}
//fix without extensions
public static IEnumerable<KeyValuePair<string, string>> GetHeaders(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]]);
}
}
foreach (var x in GetHeaders(h))
{
Console.WriteLine(x.Key + ": " + x.Value);
}
foreach(string item in response.Headers) {
string key = item.ToString();
if (key == "x-ng-sessionid") {
sessionid = response.Headers[item];
}
}
精彩评论