title: Using CookieContainer with WebClient class
body: <p>I've previously used a CookieContainer with HttpWebRequest and HttpWebResponse sessions, but now, I want to use it with a WebClient. As far as I understand, there is no built-in method like there is for HttpWebRequests (<code>request.CookieContainer</code>). <strong>How can I collect cookies from a WebClient in a CookieContainer?</strong></p>
  
  <p>I <a href="http://www.google.com/search?rlz=1C1GGLS_enUS308US308&amp;sourceid=chrome&amp;ie=UTF-8&amp;q=webclient.cookiecontainer" rel="noreferrer">googled</a> for this and found <a href="http://couldbedone.blogspot.com/2007/08/webclient-handling-cookies.html" rel="noreferrer">the following sample</a>:</p>
  
  <pre><code>public class CookieAwareWebClient : WebClient
  {
      private readonly CookieContainer m_container = new CookieContainer();
  
      protected override WebRequest GetWebRequest(Uri address)
      {
          WebRequest request = base.GetWebRequest(address);
          HttpWebRequest webRequest = request as HttpWebRequest;
          if (webRequest != null)
          {
              webRequest.CookieContainer = m_container;
          }
          return request;
      }
  }
  </code></pre>
  
  <p>Is this the best way to do it?</p>