我試着在下次啓動我的應用程序時序列化cookie來保存它並反序列化。但是反序列化的結果是空的。有什麼不對?如何在wp7應用程序中序列化CookieContainer?
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可能是Serialize.But存在WP7庫
目前在WP7沒有BinaryFormatter的,因此這個答案是不適用的。 –
我沒有意識到這一點!試試這個答案:http://stackoverflow.com/a/1777234/1099131 – LucidObscurity