码迷,mamicode.com
首页 > Windows程序 > 详细

C#保存CookieContainer到文件

时间:2016-06-13 13:30:33      阅读:304      评论:0      收藏:0      [点我收藏+]

标签:

摘自: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();  
    }  
}  

  

C#保存CookieContainer到文件

标签:

原文地址:http://www.cnblogs.com/gezifeiyang/p/5580071.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!