在WebBrowser中使用CookieContainer中的cookie

时间:2010-11-15 14:15:52

标签: c# .net winforms webbrowser-control cookiecontainer

有没有办法可以实际使用cookie容器中的cookie(以前从WebRequest中获取)并在WebBrowser控件中使用它们?如果是这样,我该怎么做?这适用于C#中的Winforms应用程序。

3 个答案:

答案 0 :(得分:10)

您需要使用InternetSetCookie。这是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); 
     }
}

答案 1 :(得分:4)

以下是如何实现这一目标的示例:

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);
    }
}

答案 2 :(得分:1)

首先尝试使用“client”CookedWebClient进行首次导航,并从服务器获取所有cookie。然后你可以从CookedWebClient或WebRequest等其他来源获取CookedContainer,并在WebBrowser中使用它们,如下所示:

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;
        }
     }
}