C# How to get cookies from GeckoWebBrowser control

前端 未结 2 765
广开言路
广开言路 2021-01-20 15:30

I am using the GeckoWebBrowser control for navigate an URL. It actually should have cookies when that page is loaded. But if I tried to get the cookie, I get a blank text ev

2条回答
  •  囚心锁ツ
    2021-01-20 16:32

    This is verified to work with GeckoFX v29.0.

    var uri = new Uri(txtURL.Text);
    //often cookies are stored on domain level, so ".google.com", not "www.google.com" (leading dot is important)
    string host = uri.Host.Replace("www", ""); 
    var cookies = CookieManager.GetCookiesFromHost(host);
    string cookiesText = "";
    while (cookies.MoveNext())
    {
        var c = cookies.Current;
        cookiesText += c.Name + "=" + c.Value + ";";
    }
    

    Also Browser.Document.Cookie seems to be more reliable now, but I haven't tested it extensively.

提交回复
热议问题