How to Get All Cookies of a Cookiecontainer

How can I get all Cookies of a CookieContainer?

A solution using reflection:

public static CookieCollection GetAllCookies(CookieContainer cookieJar)
{
CookieCollection cookieCollection = new CookieCollection();

Hashtable table = (Hashtable) cookieJar.GetType().InvokeMember("m_domainTable",
BindingFlags.NonPublic |
BindingFlags.GetField |
BindingFlags.Instance,
null,
cookieJar,
new object[] {});

foreach (var tableKey in table.Keys)
{
String str_tableKey = (string) tableKey;

if (str_tableKey[0] == '.')
{
str_tableKey = str_tableKey.Substring(1);
}

SortedList list = (SortedList) table[tableKey].GetType().InvokeMember("m_list",
BindingFlags.NonPublic |
BindingFlags.GetField |
BindingFlags.Instance,
null,
table[tableKey],
new object[] { });

foreach (var listKey in list.Keys)
{
String url = "https://" + str_tableKey + (string) listKey;
cookieCollection.Add(cookieJar.GetCookies(new Uri(url)));
}
}

return cookieCollection;
}


.NET 6 Update

Finally, .NET 6 was released and introduced the CookieContainer.GetAllCookies() method which extracts the CookieCollection - Documentation link.

public System.Net.CookieCollection GetAllCookies();

How to get cookies info inside of a CookieContainer? (All Of Them, Not For A Specific Domain)

reflection can be used to get the private field that holds all the domain key in CookieContainer object,

Q. How do i got the name of that private field ?

Ans. Using Reflector;

its is declared as :

private Hashtable m_domainTable;

once we get the private field, we will get the the domain key, then getting cookies is a simple iteration.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Net;
using System.Collections;

namespace ConsoleApplication4
{
static class Program
{
private static void Main()
{
CookieContainer cookies = new CookieContainer();
cookies.Add(new Cookie("name1", "value1", "/", "domain1.com"));
cookies.Add(new Cookie("name2", "value2", "/", "domain2.com"));

Hashtable table = (Hashtable)cookies.GetType().InvokeMember(
"m_domainTable",
BindingFlags.NonPublic |
BindingFlags.GetField |
BindingFlags.Instance,
null,
cookies,
new object[]{}
);

foreach (var key in table.Keys)
{
Uri uri = new Uri(string.Format("http://{0}/", key));

foreach (Cookie cookie in cookies.GetCookies(uri))
{
Console.WriteLine("Name = {0} ; Value = {1} ; Domain = {2}",
cookie.Name, cookie.Value, cookie.Domain);
}
}

Console.Read();
}
}
}

How convert CookieContainer to string?

If you are only interested in the cookies for a specific domain, then you can iterate using the GetCookies() method.

var cookieContainer = new CookieContainer();
var testCookie = new Cookie("test", "testValue");
var uri = new Uri("https://www.google.com");
cookieContainer.Add(uri, testCookie);

foreach (var cookie in cookieContainer.GetCookies(uri))
{
Console.WriteLine(cookie.ToString()); // test=testValue
}

If your interested in getting all the cookies, then you may need to use reflection as provided by this answer.

How can I make a copy of a CookieContainer?

Take a look at the CookieContainter class and you'll see that concurrent scenarios are suppose to occur when there are changes in the cookie collection, right?

You'll notice that the author of CookieContainer took care of using lock {} and SyncRoot all around these collection-changing parts of the code, and I don't think that such approach is not addressed to concurrent scenarios.

Also, you can notice that any added Cookie is literally cloned, so the cookies inside the container and all the operations made will not mess up with object references outside the cookie container. In the worst case of I'm missing something, the clone also gives us a tip of what exactly you have to copy and how you could do it, in case of using the reflection approach described in the other posts (I personally would not consider it a hack, since it fits the requirement and it is managed, legal and safe code :) ).

In fact, the mentions all over MSDN documentation are "Any instance members are not guaranteed to be thread safe." - its a kind of reminder, because you are right, you really need to be careful. Then with such statement you can suppose basically two things: 1) Non-static members are not safe at all. 2) Some members can be thread safe, but they aren't properly documented.

How can I get cookies from HttpClientHandler.CookieContainer

int loop1, loop2;
HttpCookieCollection MyCookieColl;
HttpCookie MyCookie;

MyCookieColl = Request.Cookies;

// Capture all cookie names into a string array.
String[] arr1 = MyCookieColl.AllKeys;

// Grab individual cookie objects by cookie name.
for (loop1 = 0; loop1 < arr1.Length; loop1++)
{
MyCookie = MyCookieColl[arr1[loop1]];
Response.Write("Cookie: " + MyCookie.Name + "<br>");
Response.Write ("Secure:" + MyCookie.Secure + "<br>");

//Grab all values for single cookie into an object array.
String[] arr2 = MyCookie.Values.AllKeys;

//Loop through cookie Value collection and print all values.
for (loop2 = 0; loop2 < arr2.Length; loop2++)
{
Response.Write("Value" + loop2 + ": " + Server.HtmlEncode(arr2[loop2]) + "<br>");
}
}


Related Topics



Leave a reply



Submit