Use cookies from CookieContainer in WebBrowser
Asked Answered
D

3

13

Is there any way that I can actually use the cookies from a cookie container (taken from a WebRequest previously) and use them in a WebBrowser control? If so, how would I do this? This is for a Winforms application in C#.

Donee answered 15/11, 2010 at 14:15 Comment(0)
P
13

You need to make use of InternetSetCookie. Here is a sample...

public partial class WebBrowserControl : Form
{
     private String url;

     [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
     public static extern bool InternetSetCookie(string lpszUrlName, string    lbszCookieName, string lpszCookieData);

     public WebBrowserControl(String path)
     {
          this.url = path;
          InitializeComponent();

          // set cookie
          InternetSetCookie(url, "JSESSIONID", Globals.ThisDocument.sessionID); 

          // navigate
          webBrowser.Navigate(url); 
     }
}
Papua answered 15/11, 2010 at 14:22 Comment(3)
I've already got the cookie container and the cookies inside it, what I need now is to use those cookies with a webBrowser control, but i'm not sure how I can use them within it. What you sent me just shows how to get the cookies out of it but doesnt describe how to set them to a browser.Donee
Thats the thing. I don't need it for a webrequest. I need it for a WebBrowser.Donee
@Donee you are trying to populate WebBrowser.Document.Cookie from your already acquired CookieContainer, is that correct?Papua
S
4

Here's an example oh how this could be achieved:

private class CookieAwareWebClient : WebClient
{
    public CookieAwareWebClient()
    {
        CookieContainer = new CookieContainer();
    }

    public CookieContainer CookieContainer { get; private set; }

    protected override WebRequest GetWebRequest(Uri address)
    {
        var request = base.GetWebRequest(address);
        var httpRequest = request as HttpWebRequest;
        if (httpRequest != null)
        {
            httpRequest.CookieContainer = CookieContainer;
        }
        return request;
    }
}


private void Form1_Load(object sender, EventArgs e)
{
    using (var client = new CookieAwareWebClient())
    {
        client.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
        client.DownloadData("http://www.google.com");
        var cookies = client.CookieContainer.GetCookies(new Uri("http://www.google.com"));
        var prefCookie = cookies["PREF"];
        webBrowser1.Navigate("http://www.google.com", "", null, "Cookie: " + prefCookie.Value + Environment.NewLine);
    }
}
Showman answered 15/11, 2010 at 15:17 Comment(0)
R
1

Try to first use "client" CookedWebClient for the first navitation and get all the cookies from server. Then you can take the CookedContainer from CookedWebClient, or some other source like WebRequest, and use them in WebBrowser as shown below:

namespace ExampleWebBrowser
{
    public partial class Form1 : Form
    {
         [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
         public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);

         CookedWebClient client = new CookedWebClient();

         ..
         ..
         ..

         private void usingWebBrowserWithWebClientCookies(string url)
         {
            CookieCollection cookies = client.Cookies.GetCookies(url);
            for (int i = 0; i < cookies.Count; i++)
            {
               Cookie c = cookies[i];
               InternetSetCookie(url, c.Name, c.Value);
            }
            webBrowser1.Navigate(url);
         }
     }

     public class CookedWebClient : WebClient
     {
        CookieContainer cookies = new CookieContainer();
        public CookieContainer Cookies { get { return cookies; } }
        protected override WebRequest GetWebRequest(Uri address)
        {
           WebRequest request = base.GetWebRequest(address);
           if (request.GetType() == typeof(HttpWebRequest))
              ((HttpWebRequest)request).CookieContainer = cookies;
           return request;
        }
     }
}
Russell answered 10/12, 2012 at 4:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.