Question

I tried to Serialize cookie to save it and Deserialize at next time I start my application.But the result of Deserialize is empty.What's wrong?

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));
            }
        }
    }
}

I also found this simple C#: Writing a CookieContainer to Disk and Loading Back In For Use shows that CookieContainer could be Serialize.But there is no SoapFormatter in wp7 library

Was it helpful?

Solution

IsolatedStorageSettings.ApplicationSettings["index"] = yourcookie;

So you don't need to serialize it.

I'm using it in a project

OTHER TIPS

As the question is "How to Serialize CookieContainer" and the accepted answer doesn't really answer this. This is how to do it with serialization:

Write to disk:

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()); 
        }
    }
}

Read from disk:

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(); 
        }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top