locked
How do I serialize a cookiecontainer to save to disk? RRS feed

  • Question

  • I'm trying to use this code in a metro app written in c#

     private void Savecookie(string filename, CookieContainer rcookie)
        {
            Stream stream = File.Open(filename, FileMode.Create);
            BinaryFormatter bFormatter = new BinaryFormatter();
            bFormatter.Serialize(stream, rcookie);
            stream.Close();
        }

    I'm able to create and save a file to disk using the xmlserializer but that doesn't serialize the cookie container correctly.  It just saves the header, but the cookie collection is empty.

    Is there an equivalent to binaryformatter?

    Sunday, July 15, 2012 3:24 AM

Answers

  • Thank you for the help.

    Apparently there is an issue with datacontractserializer and cookiecontainers.  It down't serialize the collection at all rendering it useless.  I had to write a class with a cookiecontainer serializer that goes through using the datacontractserializer and save the individual collection.

    • Marked as answer by Min ZhuMember Saturday, August 11, 2012 8:44 AM
    Thursday, July 19, 2012 9:55 PM

All replies

  • Hello

    use DataContractSerializer.

    DataContractSerializer serializer = new DataContractSerializer(CookieContainer.GetType());
        serializer.WriteObject(stream, CookieContainer);
        stream.Position = 0;

    Sorry It has not actually tried.


    Sunday, July 15, 2012 5:45 PM
  • Thank you for the help.

    Apparently there is an issue with datacontractserializer and cookiecontainers.  It down't serialize the collection at all rendering it useless.  I had to write a class with a cookiecontainer serializer that goes through using the datacontractserializer and save the individual collection.

    • Marked as answer by Min ZhuMember Saturday, August 11, 2012 8:44 AM
    Thursday, July 19, 2012 9:55 PM
  • Can you please share the code you used to do it?
    Friday, September 14, 2012 1:44 AM
  • Sure...but first I have to explain that you can't access the cookie collection anonymously, you have to know the uri of the cookies.  For me it didn't cause an issue, but it may be an issue for you....now the code.

    Here are the serialize/deserialize functions

            public static void Serialize(CookieCollection cookies, Uri address, Stream stream)
            {
                DataContractSerializer formatter = new DataContractSerializer(typeof(List<Cookie>));
                List<Cookie> cookieList = new List<Cookie>();
                for (var enumerator = cookies.GetEnumerator(); enumerator.MoveNext(); )
                {
                    var cookie = enumerator.Current as Cookie;
                    if (cookie == null) continue;
                    cookieList.Add(cookie);
    
                }
                formatter.WriteObject(stream, cookieList);
            }
                   
            public static CookieContainer Deserialize(Stream stream, Uri uri)
            {
                List<Cookie> cookies = new List<Cookie>();
                CookieContainer container = new CookieContainer();
                DataContractSerializer formatter = new DataContractSerializer(typeof(List<Cookie>));
                cookies = (List<Cookie>)formatter.ReadObject(stream);
                CookieCollection cookieco = new CookieCollection();
    
                foreach (Cookie cookie in cookies)
                {
                    cookieco.Add(cookie);
                }
                container.Add(uri, cookieco);
                return container;
            }

    And here are examples of usage

            private async void Savecookie(string filename, CookieContainer rcookie, Uri uri)
            {
                StorageFolder localFolder = ApplicationData.Current.LocalFolder;
                StorageFile sampleFile = await localFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
    
                using (StorageStreamTransaction transaction = await sampleFile.OpenTransactedWriteAsync())
                {
                    CookieSerializer.Serialize(rcookie.GetCookies(uri), uri, transaction.Stream.AsStream());
                    await transaction.CommitAsync();
                }
            }
    
            private async void Loadcookie(string filename)
            {
                StorageFolder localFolder = ApplicationData.Current.LocalFolder;
                StorageFile sampleFile;
                try
                {
                    sampleFile = await localFolder.GetFileAsync(filename);
                }
                catch
                {
                    return;
                }
    
                using (Stream stream = await sampleFile.OpenStreamForReadAsync())
                {
                    rCookie= CookieSerializer.Deserialize(stream, new Uri("http://www.google.com/"));
                }
    
            }

    Friday, September 14, 2012 1:58 AM
  • Great Example!
    Monday, December 8, 2014 1:49 PM