C# 将cookiecontainer写到本地
Posted 祝福中国,祝福中国人民
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 将cookiecontainer写到本地相关的知识,希望对你有一定的参考价值。
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写到本地的主要内容,如果未能解决你的问题,请参考以下文章
将 CookieContainer 与 WebClient 类一起使用
池化的 HttpClient 实例是不是保留 CookieContainer?
HttpWebRequest:将 Cookie 添加到 CookieContainer -> ArgumentException(参数名称:cookie.Domain)