Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 852
gusaindpk Avatar asked Jun 22 '11 11:06

gusaindpk


1 Answers

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(); 
like image 86
ony Avatar answered Oct 19 '22 19:10

ony