zoukankan      html  css  js  c++  java
  • C#遍历CookieContainer所有Cookie并保存到文件

    C#遍历CookieContainer所有Cookie并保存到文件

    在开HTTP方式提交数据时,有的要登录才能发送数据,如果每次都登录,那就有点浪费时间了,因为网络的问题,很多时间都花来回传输数据上,如果登录后把CookieContainer保存到文件或数据库那就会快很多了。
    保存CookieContainer考虑到两种方法,一种是把CookieContainer序列化,再保存,用的时候再反序列化;另一种是遍历出所有的Cookie,再把内容读出来,用的时候再构建新有Cookie,再添加到CookieContainer。经过考虑,决定用第二种,因为Cookie会有时间限制,如果单单的序列化后保存,就有可能会过期,而后一种则可以自己设置时间。

    以下的代码可以适用于很多场合,包括论坛,社区,甚至某些邮箱等。当然此处我只是举例,将内容保存到一个文本文件,而实际可可以保存到数据库或其他地方。

    首先,我们用一个方法把CookieContainer所有的Cookie读出来,此方法的代码:
    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;
            }
    然后我们再保存到文件:
     StringBuilder sbc = new StringBuilder();
                List<Cookie> cooklist = Code.ProgTool.GetAllCookies(CookieContainer);
                foreach (Cookie cookie in cooklist)
                {
                    sbc.AppendFormat("{0};{1};{2};{3};{4};{5}\r\n",
                        cookie.Domain,cookie.Name, cookie.Path, cookie.Port, 
                        cookie.Secure.ToString(), cookie.Value);
                }

                FileStream fs = File.Create("d:\\chinarencookies.txt");
                fs.Close();
                File.WriteAllText("d:\\chinarencookies.txt", sbc.ToString(), System.Text.Encoding.Default);
    读出所有Cookie
    string[] cookies = File.ReadAllText("d:\\chinarencookies.txt", System.Text.Encoding.Default).Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                foreach (string c in cookies)
                {
                    string[] cc = c.Split(";".ToCharArray());
                    Cookie ck = new Cookie(); ;
                    ck.Discard =false;
                    ck.Domain = cc[0];
                    ck.Expired =true;                
                    ck.HttpOnly = true;
                    ck.Name = cc[1];
                    ck.Path = cc[2];
                    ck.Port = cc[3];
                    ck.Secure = bool.Parse(cc[4]);
                    ck.Value = cc[5];
                    CookieContainer.Add(ck);

                } 

  • 相关阅读:
    【校招面试 之 C/C++】第23题 C++ STL(五)之Set
    Cannot create an instance of OLE DB provider “OraOLEDB.Oracle” for linked server "xxxxxxx".
    Redhat Linux安装JDK 1.7
    ORA-10635: Invalid segment or tablespace type
    Symantec Backup Exec 2012 Agent for Linux 卸载
    Symantec Backup Exec 2012 Agent For Linux安装
    You must use the Role Management Tool to install or configure Microsoft .NET Framework 3.5 SP1
    YourSQLDba介绍
    PL/SQL重新编译包无反应
    MS SQL 监控数据/日志文件增长
  • 原文地址:https://www.cnblogs.com/goody9807/p/2370080.html
Copyright © 2011-2022 走看看