CookContainer 序列化保存

using System;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography.X509Certificates;

public static bool WriteCookiesToDisk(string file, CookieContainer cookieJar)
{
//file = file.Replace(":", ":");
if (file == "") return false ;
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.");
return true;
}
catch (Exception e)
{
// Console.Out.WriteLine("Problem writing cookies to disk: " + e.GetType());
}
}
return false;
}

public static CookieContainer ReadCookiesFromDisk(string file)
{
// file = file.Replace(":", ":");
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();
return new CookieContainer();
}
}

时间: 2024-08-09 21:30:59

CookContainer 序列化保存的相关文章

使用序列化保存对象状态到存储介质

//使用序列化保存对象状态到存储介质 //添加[Serializable] Game game = new Game(); game.Level = 2; game.Player = "Tom"; FileStream fs = new FileStream(@"game.bin",FileMode.Create); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs,game); //使用反序列化

【原创】在Unity中关于多态转换与序列化保存的细节

在Unity中关于多态转换与序列化保存的细节 环境: 在unity中为类变量分类. [System.Serializable] public class dllProperty { public int num1 = 1; public string name1 = "dll"; public bool isOn = false; } [System.Serializable] public class myProperty : dllProperty { public int num

序列化保存对象

1 [Serializable] 2 public class Student 3 { 4 public Student(int age) 5 { 6 this.age = age; 7 } 8 9 private int age; 10 11 public string Name { get; set; } 12 13 } 1 static void Load() 2 { 3 using (FileStream fs = new FileStream("stu.bin", FileM

序列化保存为二进制数组 忘得干干净净

ReturnExam re = new ReturnExam(); re.Juquest = jseleqj; re.Muliselectquest = mseleqj; re.Selectquest = seleqj; re.Tiankongquest = tseleqj; re.Wendaquest = wseleqj; MemoryStream stream = new MemoryStream(); BinaryFormatter binFormat = new BinaryFormat

序列化与反序列化总结(Serializable和Parcelable)

序列化是指将对象的状态信息转换为可以存储或传输的形式的过程. 在Java中创建的对象,只要没有被回收就可以被复用,但是,创建的这些对象都是存在于JVM的堆内存中,JVM处于运行状态时候,这些对象可以复用, 但是一旦JVM停止,这些对象的状态也就丢失了. 在实际生活中,需要将对象持久化,需要的时候再重新读取出来,通过对象序列化,可以将对象的状态保存为字节数组,需要的时候再将字节数组反序列化为对象. 对象序列化可以很容易的在JVM中的活动对象和字节数组(流)之间转换,广泛用于RMI(远程方法调用)以

.net中对象序列化技术浅谈

.net中对象序列化技术浅谈 2009-03-11 阅读2756评论2 序列化是将对象状态转换为可保持或传输的格式的过程.与序列化相对的是反序列化,它将流转换为对象.这两个过程结合起来,可以轻松地存储和传输数 据.例如,可以序列化一个对象,然后使用 HTTP 通过 Internet 在客户端和服务器之间传输该对象.反之,反序列化根据流重新构造对象.此外还可以将对象序列化后保存到本地,再次运行的时候可以从本地文件 中“恢复”对象到序列化之前的状态.在.net中有提供了几种序列化的方式:二进制序列化

Java 序列化深入分析

序列化机制介绍 ??序列化是指把对象转换成有序字节流,以便在网络上传输或者保存在本地文件中.序列化后的字节流保存了Java对 象的状态以及相关的描述信息.客户端从文件中或网络上获得序列化后的对象字节流后,根据字节流中所保存的对象状态及描述信息,通过反序列化重建对象.本质上讲,序列化就是把实体对象状态按照一定的格式写入到有序字节流,反序列化就是从有序字节流重建对象,恢复对象状态.序列化机制的核心作用就是对象状态的 保存与重建. Java序列化机制解析 ??Java API提供了对序列化的支持,要实

Java对象表示方式1:序列化、反序列化和transient关键字的作用

http://www.cnblogs.com/xrq730/p/4821958.html 平时我们在Java内存中的对象,是无 法进行IO操作或者网络通信的,因为在进行IO操作或者网络通信的时候,人家根本不知道内存中的对象是个什么东西,因此必须将对象以某种方式表示出来,即 存储对象中的状态.一个Java对象的表示有各种各样的方式,Java本身也提供给了用户一种表示对象的方式,那就是序列化.换句话说,序列化只是表示对 象的一种方式而已.OK,有了序列化,那么必然有反序列化,我们先看一下序列化.反序

Java序列化(Serialization)的理解

1.什么是序列化 Java是面向对象的编程语言,有时需要保存对象,并在下次使用时可以顺利还原该对象.由于这种需求很常见,所以Java API对此提供了支持,添加相关程序代码到标准类库中,并将保存和还原的过程称之为"对象序列化". Java SE7 文档中将与对象序列化的相关内容做了详细表述,将其称为: "Java对象序列化规范"  Java Object Serialization Specification,网址为: http://docs.oracle.com/