0

I'm reposting this question with screenshots as might make my problem easier to understand.

I have a CookieContainer that is populated on a HttpWebRequest. I'm trying to iterate through the values and sort of got close with the following line of code:

  var getvalue = myWebRequest.CookieContainer.GetCookies(new Uri("https://mydummy.domain.com"));

However, out of the 4 potential values listed in the cookie collection, it always returns the last entry.

Does anyone know how I can get at the list so I an iterate through and cherry pick the value I'm after?

enter image description here

enter image description here

enter image description here

enter image description here

In the last image I've got an entry at position 2 out of the possible 4 entries (shown at array position 1). It's that value JSESSIONID that I'm trying to pull out.

Sulphy
  • 766
  • 2
  • 9
  • 29

1 Answers1

0

Eventually found this article which had an example which did exactly what I was after:

How can I get all Cookies of a CookieContainer?

Example code:

public static IEnumerable<Cookie> GetAllCookies(this CookieContainer c)
{
Hashtable k = (Hashtable)c.GetType().GetField("m_domainTable", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(c);
foreach (DictionaryEntry element in k)
{
    SortedList l = (SortedList)element.Value.GetType().GetField("m_list", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(element.Value);
    foreach (var e in l)
    {
        var cl = (CookieCollection)((DictionaryEntry)e).Value;
        foreach (Cookie fc in cl)
        {
            yield return fc;
        }
    }
}
}
Community
  • 1
  • 1
Sulphy
  • 766
  • 2
  • 9
  • 29