欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c# 获取CookieContainer的所有cookies函数代码

程序员文章站 2024-02-14 10:15:04
复制代码 代码如下:///         /// 遍历cookieco...

复制代码 代码如下:

/// <summary>
        /// 遍历cookiecontainer
        /// </summary>
        /// <param name="cc"></param>
        /// <returns></returns>
        public static list<cookie> getallcookies(cookiecontainer cc)
        {
            list<cookie> lstcookies = new list<cookie>();

            hashtable table = (hashtable)cc.gettype().invokemember("m_domaintable",
                system.reflection.bindingflags.nonpublic | system.reflection.bindingflags.getfield |
                system.reflection.bindingflags.instance, null, cc, new object[] { });

            foreach (object pathlist in table.values)
            {
                sortedlist lstcookiecol = (sortedlist)pathlist.gettype().invokemember("m_list",
                    system.reflection.bindingflags.nonpublic | system.reflection.bindingflags.getfield
                    | system.reflection.bindingflags.instance, null, pathlist, new object[] { });
                foreach (cookiecollection colcookies in lstcookiecol.values)
                    foreach (cookie c in colcookies) lstcookies.add(c);
            }
            return lstcookies;
        }