C# How to get cookies from GeckoWebBrowser control

随声附和 提交于 2019-12-01 23:17:35

问题


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 even the page is loaded.

GeckoWebBrowser m_Browser = ...

// ... after navigated. string sCookie = m_Browser.Document.Cookie.ToString();

Is there another way to get the cookies from GeckoWebBrowser? Please help me. Thanks


回答1:


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.




回答2:


i found a reason for this issue, because the all element of geckowebbrowser is loading then you set cookie immediately, so geckowebbrowser not enough time to authorized. My solution is make a button. onload winform you do navigate url you wan, after than you click on button, behind event button you set authorized cookie for your url. It's working for me. :)



来源:https://stackoverflow.com/questions/20485504/c-sharp-how-to-get-cookies-from-geckowebbrowser-control

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!