How to loop through WebHeaderCollection

Viewed 28153

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.

12 Answers

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();
foreach(string item in response.Headers) {
  string key = item.ToString();
      if (key == "x-ng-sessionid") {
         sessionid = response.Headers[item];
         }
      }
Related