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();
}
}
}
None of the answers worked for me. This is my humble solution for the problem.
public static List<Cookie> List(this CookieContainer container)
{
var cookies = new List<Cookie>();
var table = (Hashtable)container.GetType().InvokeMember("m_domainTable",
BindingFlags.NonPublic |
BindingFlags.GetField |
BindingFlags.Instance,
null,
container,
null);
foreach (string key in table.Keys)
{
var item = table[key];
var items = (ICollection) item.GetType().GetProperty("Values").GetGetMethod().Invoke(item, null);
foreach (CookieCollection cc in items)
{
foreach (Cookie cookie in cc)
{
cookies.Add(cookie);
}
}
}
return cookies;
}
Thank's to AppDeveloper for their answer, here is a slightly modified version as an extension method.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
public static class CookieContainerExtension
{
public static List<Cookie> List(this CookieContainer container)
{
var cookies = new List<Cookie>();
var table = (Hashtable)container.GetType().InvokeMember("m_domainTable",
BindingFlags.NonPublic |
BindingFlags.GetField |
BindingFlags.Instance,
null,
container,
new object[] { });
foreach (var key in table.Keys)
{
Uri uri = null;
var domain = key as string;
if (domain == null)
continue;
if (domain.StartsWith("."))
domain = domain.Substring(1);
var address = string.Format("http://{0}/", domain);
if (Uri.TryCreate(address, UriKind.RelativeOrAbsolute, out uri) == false)
continue;
foreach (Cookie cookie in container.GetCookies(uri))
{
cookies.Add(cookie);
}
}
return cookies;
}
}
To get the list just call List() on the CookieContainer:
CookieContainer cookies = new CookieContainer();
cookies.Add(new Cookie("name1", "value1", "/", "www.domain1.com"));
cookies.Add(new Cookie("name2", "value2", "/", "www.domain2.com"));
List<Cookie> cookieList = cookies.List();