C#编写的CookieContainer到磁盘和加载回在使用 [英] C#: Writing a CookieContainer to Disk and Loading Back In For Use

查看:269
本文介绍了C#编写的CookieContainer到磁盘和加载回在使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 的CookieContainer 从命名的 CookieJar 的一个的HttpWebRequest / HttpWebResponse会话提取。我想我的应用程序存储运行之间的cookies,所以在程序的运行在的CookieContainer 收集的cookie将被用于下次运行了。

I have a CookieContainer extracted from a HttpWebRequest/HttpWebResponse session named CookieJar. I want my application to store cookies between runs, so cookies collected in the CookieContainer on one run of the program will be used the next run, too.

我觉得要做到这一点的方法是将某种方式写的CookieContainer到磁盘的内容。我的问题是:

I think the way to do this would be to somehow write the contents of a CookieContainer to disk. My question is:


  • 您如何写的CookieContainer磁盘?是否有内置的功能这一点,或者,如果没有,什么是人们所采取的做法?是否有这种简化?

  • 一旦你已经写了一个的CookieContainer磁盘可用任何类,如何加载它放回使用

  • How can you write a CookieContainer to the disk? Are there built-in functions for this, or, if not, what are the approaches people have taken? Are there any classes available for simplifying this?
  • Once you've written a CookieContainer to the disk, how do you load it back in for use?

更新:第一个答案已经提出的序列化 >的CookieContainer 。不过,我不是很熟悉如何序列化和反序列化这样复杂的对象。你能否提供一些的示例代码?该建议是利用 SOAPFormatter

UPDATE: The first answer has suggested serialization of the CookieContainer. However, I am not very familiar with how to serialize and deserialize such complex objects. Could you provide some sample code? The suggestion was to utilise SOAPFormatter.

推荐答案

我没有试过它,但它具有序列化属性,因此可以是[德]序列化与.NET二进制序列化,例如: SoapFormatter。

I Haven't tried it but it has the attribute Serializable and so can be [de]serialized with .net binary serialization, e.g. SoapFormatter.

下面是代码片段,你提出的要求。

Here is the code snippet you asked for.

var formatter = new SoapFormatter();
string file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "cookies.dat"); 

using (Stream s = File.Create (file))
    formatter.Serialize(s, cookies);                            
...
CookieContainer retrievedCookies = null;
using (Stream s = File.OpenRead (file))
    retrievedCookies = (CookieContainer) formatter.Deserialize(s);



在MSDN看似乎SoapFormatter现在已经被废弃在.NET 3.5,它建议您使用的BinaryFormatter。在过去,我发现SoapFormatter有用的文件可读它与诊断有助于在反序列化失败!这些格式化的,即使在程序集版本(所以如果你的框架的一个版本升级的框架反序列化,那么它可能无法反序列化,不知道)版本的变化很敏感,但有办法解决这个与粘结剂财产,如果这成为一个问题。我相信他们的设计主要是短期持久性/远程处理,但他们可能对你不够好这里。

Looking at msdn it seems SoapFormatter is now deprecated in .net 3.5 and it recommends you use Binaryformatter. In the past I have found SoapFormatter useful as the file is readable which helps with diagnosis when deserialization fails! These formatters are sensitive to version changes even in the assembly version (so if you deserialize with one version of the framework upgrade the framework, then it might not deserialize, not sure), but there are ways around this with the Binder property if this becomes a problem. I believe they are primarily designed for short term persistance / remoting, but they might be good enough for you here.

新的DataContractSerializer似乎并没有与它合作,使出来了。

The new DataContractSerializer does not seem to work with it so that is out.

另一种方法是到CookieContainerData类写入[德]与XmlSerializer的序列化和手动这一点,的CookieContainer之间进行转换。

An alternative would be to write a CookieContainerData class to [de]serialize with XmlSerializer and manually convert between this and CookieContainer.

这篇关于C#编写的CookieContainer到磁盘和加载回在使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆