如何在wp7应用程序中序列化CookieContainer?

时间:2011-02-08 08:55:51

标签: serialization windows-phone-7 cookiecontainer

我尝试序列化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可能是Serialize.But wp7库中没有SoapFormatter

2 个答案:

答案 0 :(得分:2)

IsolatedStorageSettings.ApplicationSettings["index"] = yourcookie;

所以你不需要序列化它。

我在项目中使用它

答案 1 :(得分:-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(); 
        }
}