3

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

user3060369
  • 33
  • 1
  • 4
  • you can get cookies value using m_Browser.Document.Cookie after you loading the page. can you tell me which website you are navigating ? and you dont need to use Tostring() here because `m_Browser.Document.Cookie` itself returns string value.. – yasmuru Dec 10 '13 at 03:58
  • This website: https://trading.binaryinternational.com/#Trade .Even I loaded this site, the cookie is still blank. The version of gecko i am using is 21.0 – user3060369 Dec 10 '13 at 06:34
  • I think in that website they are securing the cookies by calling `javascripts`, did u tried with some other websites ? – yasmuru Dec 10 '13 at 06:42
  • So isn't there any other way to get cookies if they are securing the cookies? – user3060369 Dec 10 '13 at 06:43
  • Use `Firebug` addon in your browser and try to identify how they generating the cookies in that website, then you can get some idea ... If you have login credentials , then login after check for the cookies – yasmuru Dec 10 '13 at 06:45

2 Answers2

4

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.

Sire
  • 4,086
  • 4
  • 39
  • 74
0

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. :)

  • This is my code: private void frm_testwebdriver_Load(object sender, EventArgs e) { Xpcom.Initialize("Firefox"); geckowebsite.Navigate("m.facebook.com"); } private void btn_Click(object sender, EventArgs e) { string[] arr1 = "your cookie"; string[] arr = arr1.Split(';'); for (int i = 0; i < arr.Length - 1; i++) { string cookieName = arr[i].Split('=')[0]; string cookieValue = arr[i].Split('=')[1]; geckowebsite.Document.Cookie = string.Format("{0}={1}; {2}", cookieName, cookieValue, geckowebsite.Document.Cookie); } geckowebsite.Navigate("m.facebook.com"); } – Thanh Le Pro Oct 30 '19 at 14:50
  • 3
    Please put your code *directly in your answer*, **not** as a comment under it. – Das_Geek Oct 30 '19 at 14:55