title: Get a CookieContainer from a WebBrowser control?
body: <p>First of all, excuse me If I misunderstood some concepts, since web requests and cookies are not my fort.</p>
  
  <p>Well, the thing is that I would like to get all the cookies stored in the <strong>WebBrowser</strong>'s session for the current url. This will help me for example after interactively logging in a website, I can retrieve the cookies from the WebBrowser instance to use them in background with the <strong>HttpWebRequest</strong> class. (the website has security measures like a captcha and a random question, so for me is not viable to try automate it using requests in background to login and store the cookies, I prefer to use a webbrowser)</p>
  
  <p>This is what I tried so far, it seems that it works fine but I'm not sure whether I did it all ok without missing any important thing to take into account, and I think my approach seems a little dirty solution, I mean maybe exists a direct way to get this from the abstraction layer of the <strong>WebBrowser</strong> control...</p>
  
  <pre><code>&lt;HideModuleName&gt;
  Public Module WebBrowserExtensions
  
      &lt;Extension&gt;
      &lt;DebuggerStepThrough&gt;
      Public Function GetCookieContainer(ByVal wb As WebBrowser) As CookieContainer
          Dim uri As Uri = wb.Url
          Dim cookieContainer As New CookieContainer()
          Dim cookies As String() = wb.Document.Cookie.Split({";"c}, StringSplitOptions.None)
  
          For Each cookie As String In cookies
              Dim name As String = cookie.Substring(0, cookie.IndexOf("="c)).TrimStart(" "c)
              Dim value As String = cookie.Substring(cookie.IndexOf("="c) + 1)
              cookieContainer.Add(uri, New Cookie(name, value, "/", uri.Host))
          Next cookie
  
          Return cookieContainer
      End Function
  
  End Module
  </code></pre>
  
  <p>So, my question is: It is all ok with my approach?, if not then which would be the proper way to do this?.</p>
  
  <p>PS: I know the <strong>WebBrowser</strong> is a embedded instance of <strong>Internet Explorer</strong>, but I'm not sure about how the <code>WebBrowser</code> manages the cookies, I mean if the cookies persists only during the lifetime of the current application or the <strong>WebBrowser</strong> instance, or just the cookies are stored in the system and are the same cookies used by <strong>Internet Explorer</strong>. As I said, excuse me If I misunderstood some concepts.</p>