我想用一个Newtonsoft.Json到的CookieContainer导出到JSON可惜的CookieContainer具有不枚举或东西,所以我无法通过它循环...
编辑:随着我发布的解决方案,将是这样的:
private static void Main(string[] args)
{
CookieContainer cookieContainer = new CookieContainer();
cookieContainer.Add(new Cookie("name1", "value1", "/", ".testdomain1.com"));
cookieContainer.Add(new Cookie("name2", "value1", "/path1/", ".testdomain1.com"));
cookieContainer.Add(new Cookie("name2", "value1", "/path1/path2/", ".testdomain1.com"));
cookieContainer.Add(new Cookie("name1", "value1", "/", ".testdomain2.com"));
cookieContainer.Add(new Cookie("name2", "value1", "/path1/", ".testdomain2.com"));
cookieContainer.Add(new Cookie("name2", "value1", "/path1/path2/", ".testdomain2.com"));
CookieCollection cookies = GetAllCookies(cookieContainer);
Console.WriteLine(JsonConvert.SerializeObject(cookies, Formatting.Indented));
Console.Read();
}
使用反射的溶液:
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;
}
第一个答案没有为便携式项目。 因此,这里的选项2,还采用了反射
using System.Linq;
using System.Collections;
using System.Reflection;
using System.Net;
public static CookieCollection GetAllCookies(this CookieContainer container)
{
var allCookies = new CookieCollection();
var domainTableField = container.GetType().GetRuntimeFields().FirstOrDefault(x => x.Name == "m_domainTable");
var domains = (IDictionary)domainTableField.GetValue(container);
foreach (var val in domains.Values)
{
var type = val.GetType().GetRuntimeFields().First(x => x.Name == "m_list");
var values = (IDictionary)type.GetValue(val);
foreach (CookieCollection cookies in values.Values)
{
allCookies.Add(cookies);
}
}
return allCookies;
}
1)I也尝试
var domainTableField = container.GetType().GetRuntimeField("m_domainTable");
但它返回null。
2)您可以通过所有密钥domains.Keys和使用container.GetCookies()迭代。 但我已经与问题,因为预计的getCookies开放的,而不是我的所有钥匙匹配URI模式。
这种方法将确保获得所有的cookies,无论该协议是什么:
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;
}
}
}
}
使用CookieContainer.GetCookies方法
CookieCollection cookies = cookieContainer.GetCookies(new Uri(url));
这里url
是您的网站的网址。
就我而言,我是不能够使用反射,在其他的答案建议。 不过,我也知道我的网站来查询的URL。 我认为这是合乎逻辑的,即使该容器未返回所有Cookie盲目,但返回他们每个URL,因为饼干永远属于一个特定的URL,不能与它们相关联的域的上下文之外使用。