0

I asked this question a while ago but seems that there are no answers, so i tried to go with an alternative solution but i am stuck now, please see the following code:

        WebBrowser objWebBrowser = new WebBrowser();
        objWebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(objWebBrowser_DocumentCompleted);
        objWebBrowser.Navigate("http://www.website.com/login.php?user=xxx&pass=xxx");
        objWebBrowser.Navigate("http://www.website.com/page.php?link=url");

And here is the event code:

        WebBrowser objWebBrowser = (WebBrowser)sender;
        String data = new StreamReader(objWebBrowser.DocumentStream).ReadToEnd();

Since it's impossible for me to use the WebBrowser.Document.Cookies before a document is loaded, i have first to navigate the login page, that will store a cookie automatically, but after that i want to call the other navigate in order to get a result. Now using the above code it doesn't work cause it always takes the second one, and it won't work for me to put it in the event cause what i want is like this:

  • Navigate with the login page and store cookie for one time only.
  • Pass a different url each time i want to get some results.

Can anybody give a solution ?

Edit:

Maybe the sample of code i provided was misleading, what i want is:

foreach(url in urls)
{
  Webborwser1.Navigate(url);
  //Then wait for the above to complete and get the result from the event, then continue
}
Community
  • 1
  • 1
ykh
  • 1,775
  • 3
  • 31
  • 57

1 Answers1

0

I think you want to simulate a blocking call to Navigate if you are not authorized. There are probably many ways to accomplish this and other approaches to get what you want, but here's some code I wrote up quickly that might help you get started.

If you have any questions about what I'm trying to do here, let me know. I admit it feels like "a hack" which makes me think there's a smarter solution, but anyway....

bool authorized = false;
bool navigated;
WebBrowser objWebBrowser = new WebBrowser();
void GetResults(string url)
{
    if(!authorized)
    {
        NavigateAndBlockWithSpinLock("http://www.website.com/login.php?user=xxx&pass=xxx");
        authorized = true;
    }
    objWebBrowser.Navigate(url);
}

void NavigateAndBlockWithSpinLock(string url)
{
    navigated = false;

    objWebBrowser.DocumentCompleted += NavigateDone;

    objWebBrowser.Navigate(url);

    int count = 0;
    while(!navigated && count++ < 10)
        Thread.Sleep(1000);

    objWebBrowser.DocumentCompleted -= NavigateDone;

    if(!navigated)
        throw new Exception("fail");
}

void NavigateDone(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    navigated = true;
}

void objWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if(authorized)
    {
        WebBrowser objWebBrowser = (WebBrowser)sender;
        String data = new StreamReader(objWebBrowser.DocumentStream).ReadToEnd();
    }
}
Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121