• C#保存CookieContainer到文件


    摘自:http://licstar.net/archives/6

    C# save CookieContainer to file”,发现了一份优雅的代码。
    http://stackoverflow.com/questions/1777203/c-writing-a-cookiecontainer-to-disk-and-loading-back-in-for-use

    public static void WriteCookiesToDisk(string file, CookieContainer cookieJar)  
    {  
        using(Stream stream = File.Create(file))  
        {  
            try {  
                Console.Out.Write("Writing cookies to disk... ");  
                BinaryFormatter formatter = new BinaryFormatter();  
                formatter.Serialize(stream, cookieJar);  
                Console.Out.WriteLine("Done.");  
            } catch(Exception e) {  
                Console.Out.WriteLine("Problem writing cookies to disk: " + e.GetType());  
            }  
        }  
    }     
      
    public static CookieContainer ReadCookiesFromDisk(string file)  
    {  
        try {  
            using(Stream stream = File.Open(file, FileMode.Open))  
            {  
                Console.Out.Write("Reading cookies from disk... ");  
                BinaryFormatter formatter = new BinaryFormatter();  
                Console.Out.WriteLine("Done.");  
                return (CookieContainer)formatter.Deserialize(stream);  
            }  
        } catch(Exception e) {  
            Console.Out.WriteLine("Problem reading cookies from disk: " + e.GetType());  
            return new CookieContainer();  
        }  
    }  
    

      

  • 相关阅读:
    ch_6802 車的放置
    ch_POJ2182 Lost Cows
    ch_4201 楼兰图腾
    luogu_P3368【模板】树状数组 2
    门面
    建造者
    模板方法
    状态
    抽象工厂
    工厂方法
  • 原文地址:https://www.cnblogs.com/gezifeiyang/p/5580071.html
Copyright © 2020-2023  润新知