2011-02-08 86 views
1

我試着在下次啓動我的應用程序時序列化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庫

回答

3
IsolatedStorageSettings.ApplicationSettings["index"] = yourcookie; 

所以你不需要它序列沒有SoapFormatter

我使用它在一個項目

-2

由於問題是「如何序列的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(); 
     } 
} 
+0

目前在WP7沒有BinaryFormatter的,因此這個答案是不適用的。 –

+1

我沒有意識到這一點!試試這個答案:http://stackoverflow.com/a/1777234/1099131 – LucidObscurity