我尝试序列化cookie以保存它并在下次启动我的应用程序时反序列化。但是反序列化的结果是空的。出了什么问题?
void SaveCookie() {
var appStorage = IsolatedStorageFile.GetUserStoreForApplication();
if (this.checkBox_save_passowrd.IsChecked == true)
{
CookieContainer cc = SEC_Services.Httprequest.cookie;
string fileName = "usercookie.xml";
using (var file = appStorage.OpenFile(fileName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
{
using (var writer = new StreamWriter(file))
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(CookieContainer));
xs.Serialize(writer, cc);
writer.Close();
}
}
}
else {
if (appStorage.FileExists("usercookie.xml"))
{
appStorage.DeleteFile("usercookie.xml");
}
}
}
void ReadCookie() {
var appStorage = IsolatedStorageFile.GetUserStoreForApplication();
if (appStorage.FileExists("usercookie.xml"))
{
using (System.IO.StreamReader reader = new StreamReader(appStorage.OpenFile("usercookie.xml", FileMode.Open)))
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(CookieContainer));
CookieContainer obj = (CookieContainer)xs.Deserialize(reader);
reader.Close();
SEC_Services.Httprequest.cookie = obj;
if (obj.Count != 0) {
NavigationService.Navigate(new Uri("/PanoramaPage.xaml", UriKind.Relative));
}
}
}
}
我还发现了这个简单
的 C#:Writing a CookieContainer to Disk and Loading Back In For Use
表明 CookieContainer 可以序列化。但是SoapFormatter
wp7 库中没有