After adding a WebBrowser control to my form (VS 2005), and the
following 2 lines of code to a click event:
webBrowser1.Navigate(new Uri(NextURL));
String PageData = webBrowser1.DocumentText;
After stepping through the code, noticed that PageData was empty.
After the click event completed processing and exited, the web page was
properly displayed in my form. After entering the click event a 2nd time
and stepping around the Navigate line (so it was not executed), and
executing the next line, discovered that PageData appeared to hold the
correct contents of the web page.
So, my question is - does the click event always have to exit before
the web page can be properly loaded, or is there a way for the Navigate
function to be called synchronously and for the program to wait until the
web page is completely loaded before attempting to access the DocumentText
property? I'm aware that there is an event called DocumentCompleted that
can be used to notify my program when the document has finished loading and
the DocumentText property is ready to be queried.
I'm not really interested in displaying the contents of the web page.
I'm
using the WebBrowser control because I'm working with a web page that
uses cookies and the WebBrowser control appears to handle those cookies
properly by default much like IE. I tried using the WebRequest and
WebResponse classes, but then discovered that these classes don't handle
cookies by default. What I'm really after is obtaining the content of the
web page into a String object so some of the content can be parsed and saved
to a text file.
Thanks for any help in advance.
Bob Bryan
Jim Hughes - 30 Dec 2006 07:10 GMT
Navigate is an async method.
You need to wait for Navigate to complete before accessing the retrieved
data. You can use the Navigated event for that.
Here is skeleton example:
Imports System.Diagnostics
Public Class Form1
Dim sw As Stopwatch
Private Sub WebBrowser1_Navigated(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.WebBrowserNavigatedEventArgs) Handles
WebBrowser1.Navigated
' Format and display the TimeSpan value.
Dim ts As TimeSpan = sw.Elapsed
Label1.Text = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", _
ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
sw = Stopwatch.StartNew
Me.WebBrowser1.Navigate("http://www.yahoo.com")
End Sub
End Class
> After adding a WebBrowser control to my form (VS 2005), and the
> following 2 lines of code to a click event:
[quoted text clipped - 30 lines]
>
> Bob Bryan
Jim Hughes - 30 Dec 2006 16:45 GMT
Should be DocumentCompleted Event, not Navigated...
Up too late...
> Navigate is an async method.
>
[quoted text clipped - 58 lines]
>>
>> Bob Bryan