Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have written a CookieSerializer that specifically address this issue. The serializer is pasted below. For a working project and scenario, please visit the project's <a href="http://cookiemanagement.codeplex.com/" rel="nofollow">CodePlex site</a>.</p> <pre><code>public static class CookieSerializer { /// &lt;summary&gt; /// Serializes the cookie collection to the stream. /// &lt;/summary&gt; /// &lt;param name="cookies"&gt;You can obtain the collection through your &lt;see cref="CookieAwareWebClient"&gt;WebClient&lt;/see&gt;'s &lt;code&gt;CookieContainer.GetCookies(Uri)&lt;/code&gt;-method.&lt;/param&gt; /// &lt;param name="address"&gt;The &lt;see cref="Uri"&gt;Uri&lt;/see&gt; that produced the cookies&lt;/param&gt; /// &lt;param name="stream"&gt;The stream to which to serialize&lt;/param&gt; public static void Serialize(CookieCollection cookies, Uri address, Stream stream) { using (var writer = new StreamWriter(stream)) { for (var enumerator = cookies.GetEnumerator(); enumerator.MoveNext();) { var cookie = enumerator.Current as Cookie; if (cookie == null) continue; writer.WriteLine(address.AbsoluteUri); writer.WriteLine(cookie.Comment); writer.WriteLine(cookie.CommentUri == null ? null : cookie.CommentUri.AbsoluteUri); writer.WriteLine(cookie.Discard); writer.WriteLine(cookie.Domain); writer.WriteLine(cookie.Expired); writer.WriteLine(cookie.Expires); writer.WriteLine(cookie.HttpOnly); writer.WriteLine(cookie.Name); writer.WriteLine(cookie.Path); writer.WriteLine(cookie.Port); writer.WriteLine(cookie.Secure); writer.WriteLine(cookie.Value); writer.WriteLine(cookie.Version); } } } /// &lt;summary&gt; /// Deserializes &lt;see cref="Cookie"&gt;Cookie&lt;/see&gt;s from the &lt;see cref="Stream"&gt;Stream&lt;/see&gt;, /// filling the &lt;see cref="CookieContainer"&gt;CookieContainer&lt;/see&gt;. /// &lt;/summary&gt; /// &lt;param name="stream"&gt;Stream to read&lt;/param&gt; /// &lt;param name="container"&gt;Container to fill&lt;/param&gt; public static void Deserialize(Stream stream, CookieContainer container) { using (var reader = new StreamReader(stream)) { while (!reader.EndOfStream) { var uri = Read(reader, absoluteUri =&gt; new Uri(absoluteUri, UriKind.Absolute)); var cookie = new Cookie(); cookie.Comment = Read(reader, comment =&gt; comment); cookie.CommentUri = Read(reader, absoluteUri =&gt; new Uri(absoluteUri, UriKind.Absolute)); cookie.Discard = Read(reader, bool.Parse); cookie.Domain = Read(reader, domain =&gt; domain); cookie.Expired = Read(reader, bool.Parse); cookie.Expires = Read(reader, DateTime.Parse); cookie.HttpOnly = Read(reader, bool.Parse); cookie.Name = Read(reader, name =&gt; name); cookie.Path = Read(reader, path =&gt; path); cookie.Port = Read(reader, port =&gt; port); cookie.Secure = Read(reader, bool.Parse); cookie.Value = Read(reader, value =&gt; value); cookie.Version = Read(reader, int.Parse); container.Add(uri, cookie); } } } /// &lt;summary&gt; /// Reads a value (line) from the serialized file, translating the string value into a specific type /// &lt;/summary&gt; /// &lt;typeparam name="T"&gt;Target type&lt;/typeparam&gt; /// &lt;param name="reader"&gt;Input stream&lt;/param&gt; /// &lt;param name="translator"&gt;Translation function - translate the read value into /// &lt;typeparamref name="T"/&gt; if the read value is not &lt;code&gt;null&lt;/code&gt;. /// &lt;remarks&gt;If the target type is &lt;see cref="Uri"&gt;Uri&lt;/see&gt; , the value is considered &lt;code&gt;null&lt;/code&gt; if it's an empty string.&lt;/remarks&gt; &lt;/param&gt; /// &lt;param name="defaultValue"&gt;The default value to return if the read value is &lt;code&gt;null&lt;/code&gt;. /// &lt;remarks&gt;The translation function will not be called for null values.&lt;/remarks&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; private static T Read&lt;T&gt;(TextReader reader, Func&lt;string, T&gt; translator, T defaultValue = default(T)) { var value = reader.ReadLine(); if (value == null) return defaultValue; if (typeof(T) == typeof(Uri) &amp;&amp; String.IsNullOrEmpty(value)) return defaultValue; return translator(value); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload