views:

272

answers:

1

Hi, I'm trying to add cookies to a webbrowser from a cookiecollection. I add them as they should be added but then they are not sent when the request is done. Here is the code I have done hitherto:

        public void myNav(string url, CookieCollection cc)
        {

            foreach (Cookie cook in cc)
            {
                webBrowser1.Document.Cookie = cook.ToString();
            }

            this.webBrowser1.Navigate(url);
            Clipboard.SetText(webBrowser1.Document.Cookie);
        }

If I copy the cookies to the clipboard they are sent correctly, but If I analyse the request with Wireshark they are not sent in the headers.

How can I solve this?

Thanks in advance, Ivan

A: 

@Sky Sanders That is what I tried, but it didn't work either.

    public void myNav(string url, CookieCollection cc)
    {

        if (this.webBrowser1.Document == null)
        {
            //No cookies in here
            MessageBox.Show("Document null -> Loading document");
            this.webBrowser1.Navigate(url);
        }
        while (this.webBrowser1.Document == null)
        {
            //Wait until document is not null
            Application.DoEvents();
        }

        foreach (Cookie cook in cc)
        {
            //Add cookies
            webBrowser1.Document.Cookie = cook.ToString();
        }

        //Navigate again
        this.webBrowser1.Navigate(url);
        Clipboard.SetText(webBrowser1.Document.Cookie);
    }
Ivan
first of all, welcome to SO. second, edit your question, don't add a non-anwer, third, you need to handle the NavigationComplete event (been a while, not sure if that is the exact name) of the browser, not just wait until doc!=null. google a WebBrowser control example and work through it, then you will find that what I suggest will get you where you want to go. good luck.
Sky Sanders