本文介绍了我如何获得的CookieContainer的所有Cookie吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用Newtonsoft.Json一个的CookieContainer导出到JSON可惜的CookieContainer还没有一个枚举器或东西,所以我可以通过它不是循环......
编辑:使用我张贴的解决方案,将是这样的:
私有静态无效的主要(字串[] args)
{
的CookieContainer的CookieContainer =新的CookieContainer();
cookieContainer.Add(新的Cookie(名1,值1,/,.testdomain1.com));
cookieContainer.Add(新的Cookie(名2,值1,/路径1 /,.testdomain1.com));
cookieContainer.Add(新的Cookie(名2,值1,/路径1 /路径/,.testdomain1.com));
cookieContainer.Add(新的Cookie(名1,值1,/,.testdomain2.com));
cookieContainer.Add(新的Cookie(名2,值1,/路径1 /,.testdomain2.com));
cookieContainer.Add(新的Cookie(名2,值1,/路径1 /路径/,.testdomain2.com));
CookieCollection饼干= GetAllCookies(的CookieContainer);
Console.WriteLine(JsonConvert.SerializeObject(饼干,Formatting.Indented));
Console.Read();
}
解决方案
使用Reflection的解决方案:
公共静态CookieCollection GetAllCookies(的CookieContainer cookieJar)
{
CookieCollection cookieCollection =新CookieCollection();
哈希表的表=(哈希表)cookieJar.GetType()。InvokeMember(m_domainTable
BindingFlags.NonPublic可|
BindingFlags.GetField |
BindingFlags.Instance,
空值,
饼干罐,
新对象[] {});
的foreach(在table.Keys VAR tableKey)
{
字符串str_tableKey =(字符串)tableKey;
如果(str_tableKey [0] =='。')
{
str_tableKey = str_tableKey.Substring(1);
}
排序列表清单=(排序列表)表[tableKey] .GetType()。InvokeMember(m_list
BindingFlags.NonPublic可|
BindingFlags.GetField |
BindingFlags.Instance,
空值,
表[tableKey]
新对象[] {});
的foreach(在list.Keys VAR listKey)
{
字符串URL =https://开头+ str_tableKey +(字符串)listKey;
cookieCollection.Add(cookieJar.GetCookies(新的URI(URL)));
}
}
返回cookieCollection;
}
I want to export a CookieContainer to JSON using Newtonsoft.Json but unfortunately CookieContainer hasn't an enumerator or stuff, so I can't cycle through it ...
Edit: With my posted solution it would be something like this:
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();
}
解决方案
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;
}
这篇关于我如何获得的CookieContainer的所有Cookie吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!