C# WebBrowser control

Asked By O O
25-Aug-08 12:24 PM
Earn up to 0 extra points for answering this tough question.
Hi, I'm working with C# WebBrowser control. The goal is scraping html using WebBrowser. I wish to work with WebClient or WebRequest, but I'm scraping websites which contains JavaScript and can dynamically change. () I'm facing with Internet connectivity problems, like "connection lost" or "navigation canceled". I wonder why WebBrowser is not notifying about these problems! I can not catch any exception when WebBrowser fails to load a website. I tried to check WebBrowser's URL for containing "res://ieframe.dll?xxxx.htm". But I think it's not a good solution. Question: How to catch exception when WebBrowser fails to navigate or load? Particularly, when does Internet suddenly go off and WebBrowser can not load html? I want to create more responsible UI, that can notify to user when Internet is not available. Orif.

  Try this

ram kumar replied to O O
25-Aug-08 12:32 PM

Hi,

c#
[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)]
[DockingAttribute(DockingBehavior.AutoDock)]
[PermissionSetAttribute(SecurityAction.InheritanceDemand, Name = "FullTrust")]
[PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
public class WebBrowser : WebBrowserBase


The WebBrowser control lets you host Web pages and other browser-enabled documents in your Windows Forms applications. You can use the WebBrowser control, for example, to provide integrated HTML-based user assistance or Web browsing capabilities in your application. Additionally, you can use the WebBrowser control to add your existing Web-based controls to your Windows Forms client applications.

Important Note:
The WebBrowser control is resource-intensive. Be sure to call the Dispose()()() method when you are finished using the control to ensure that all resources are released in a timely fashion. You must call the Dispose()()() method on the same thread that attached the events, which should always be the message or user-interface (UI) thread.
 

The WebBrowser control cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.

The WebBrowser control has several properties, methods, and events related to navigation. The following members let you navigate the control to a specific URL, move backward and forward through the navigation history list, and load the home page and search page of the current user:

Url

Navigate

GoBack

GoForward

GoHome

GoSearch

If the navigation is unsuccessful, a page indicating the problem is displayed. Navigation with any of these members causes the Navigating, Navigated, and DocumentCompleted events to occur at different stages of navigation.

These and other members, such as the Stop and Refresh methods, let you implement user interface controls in your application similar to those in Internet Explorer. Some members are useful even when you do not want to display the WebBrowser control on your form. For example, you can use the Print method to print the latest version of a Web page without displaying the page to the user.

The WebBrowser control also lets you display content that you create in your application or you retrieve from a database or resource file. Use the DocumentText or DocumentStream property to get or set the contents of the current document as a string or data stream.

You can also manipulate the contents of a Web page through the Document property, which contains an HtmlDocument object that provides managed access to the HTML document object model (DOM) for the current page. This property is useful, when used in combination with the ObjectForScripting property, to implement two-way communication between your application code and dynamic HTML (DHTML) code in a Web page, letting you combine Web-based controls and Windows Forms controls in a single user interface. You can use the Document property to call scripting code methods from your application. Your scripting code can access your application through the window.external object, which is a built-in DOM object provided for host access, and which maps to the object that you specify for the ObjectForScripting property.

Note:
This class makes security demands at the class level. A SecurityException is thrown when a derived class or any caller in the call stack does not have full trust permission. For details about security demands, see Link Demands and Inheritance Demands.
 
Note:
The WebBrowser class can only be used in threads set to single thread apartment (STA) mode. To use this class, ensure that your Main method is marked with the STAThreadAttribute attribute.
 

Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE Platform Note:

Full functionality for the WebBrowser in .NET Compact Framework applications requires Windows Mobile version 5.0 software for Pocket PCs and Smartphones. For more information, see How to: Use the WebBrowser Control in the .NET Compact Framework.

 Examples
The following code example demonstrates how to implement an address bar for use with the WebBrowser control. This example requires that you have a form that contains a WebBrowser control called webBrowser1, a TextBox control called TextBoxAddress, and a Button control called ButtonGo. When you type a URL into the text box and press ENTER or click the Go button, the WebBrowser control navigates to the URL specified. When you navigate by clicking a hyperlink, the text box automatically updates to display the current URL.

// Navigates to the URL in the address box when
// the ENTER key is pressed while the ToolStripTextBox has focus.
private void toolStripTextBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        Navigate(toolStripTextBox1.Text);
    }
}

// Navigates to the URL in the address box when
// the Go button is clicked.
private void goButton_Click(object sender, EventArgs e)
{
    Navigate(toolStripTextBox1.Text);
}

// Navigates to the given URL if it is valid.
private void Navigate(String address)
{
    if (String.IsNullOrEmpty(address)) return;
    if (address.Equals("about:blank")) return;
    if (!address.StartsWith("http://") &&
        !address.StartsWith("https://"))
    {
        address = "http://" + address;
    }
    try
    {
        webBrowser1.Navigate(new Uri(address));
    }
    catch (System.UriFormatException)
    {
        return;
    }
}

// Updates the URL in TextBoxAddress upon navigation.
private void webBrowser1_Navigated(object sender,
    WebBrowserNavigatedEventArgs e)
{
    toolStripTextBox1.Text = webBrowser1.Url.ToString();
}

 

 

  Don't copy paste!

Peter Bromberg replied to ram kumar
25-Aug-08 12:40 PM

Anybody can copy / paste content from MSDN or some article on another site. If you want to do this, provide the actual url:

http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.aspx

This isn't contributing an original answer to solve the person's problem.

  Re:

O O replied to ram kumar
25-Aug-08 12:41 PM
Hi Ram, Thanks for the fast reply. I appreciate when people tries to help me. The problem is how to catch a browser event (I don't know how to name it) when Internet connection goes off. I want to catch an exception like "Internet connection died..." or "Remote host is not available...". Please, help me.
  try this
Web Star replied to O O
25-Aug-08 11:39 PM
DAMAGED RESOLUTION OF A NON-EXISTENT DOMAIN
[jack@gracie jack]$ dig -t mx aerlgjqkgjn.com
; <<>> DiG 9.2.1 <<>> -t mx aerlgjqkgjn.com
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 30438
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0
;; QUESTION SECTION:
;aerlgjqkgjn.com.               IN      MX
;; AUTHORITY SECTION:
com.                    10800   IN      SOA     a.gtld-servers.net. nstld.verisign-grs.com. 2003091600 1800 900 604800 86400
;; Query time: 95 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Tue Sep 16 09:21:43 2003
;; MSG SIZE  rcvd: 106
==========================================================================================================================
[jack@gracie jack]$ dig -t a aerlgjqkgjn.com
; <<>> DiG 9.2.1 <<>> -t a aerlgjqkgjn.com
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 27464
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 13, ADDITIONAL: 13
;; QUESTION SECTION:
;aerlgjqkgjn.com.               IN      A
;; ANSWER SECTION:
aerlgjqkgjn.com.        900     IN      A       64.94.110.11
;; AUTHORITY SECTION:
com.                    172800  IN      NS      l.gtld-servers.net.

;; Query time: 121 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Tue Sep 16 09:35:36 2003
;; MSG SIZE  rcvd: 481
also see this link
http://www.monkeynoodle.org/comp/verisign
Create New Account