1

我尝试序列化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 可以序列化。但是SoapFormatterwp7 库中没有

4

2 回答 2

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

所以你不需要序列化它。

我在一个项目中使用它

于 2011-11-09T21:39:23.183 回答
-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(); 
        }
}
于 2012-01-15T06:14:56.160 回答