Trying to implement the following code:

using (WebBrowser brows = new WebBrowser()) { brows.Navigate(new Uri(this.url)); richTextBox1.Text = brows.DocumentText.ToString(); } 

However, brows.DocumentText contains nothing. There is no browser on the form. How to fix the problem?

  • one
    > There is no browser on the form. Hmm, where do they usually appear from there? - VladD

2 answers 2

Use webpage load events.

 brows.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(brows_DocumentCompleted); brows.Navigate(new Uri("http://google.ru", UriKind.Absolute)); while (brows.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); } void brows_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { richTextBox1.Text = (sender as WebBrowser).Document.Body.InnerText; } 

    There will be nothing on the form, considering the using construction. You really understand why you use it?

    At least the page has not yet had time to load.

    If you need Webbrowser on the form, put it there (either from the Toolbox transfer or in the code

     var web = new WebBrowser Controls.Add(web); 

    If you just need to download the content by URL, it is better to use the WebClient. DownloadData