我如何获得的CookieContainer的所有Cookie吗?如何获得、CookieContainer、Cookie

2023-09-02 01:50:27 作者:Hollow(空心)

我想用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 =htt​​ps://开头+ str_tableKey +(字符串)listKey;
            cookieCollection.Add(cookieJar.GetCookies(新的URI(URL)));
        }
    }

    返回cookieCollection;
}
 

PHP第一次获取不到cookie的值,我知道原因,但是怎样可以第一次就获取到cookie的值,代码如下

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;
}

 
精彩推荐
图片推荐