Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27350

VS 2010 InternetExplorer vs. WebClient vs. WebRequest guidelines and/or best practices?

$
0
0
I'm using VB.Net 2010 with Framework 4.0 to create an application that gets information from a webpage. I've coded the application two ways:

(1) The first version automates a hidden instance of the Internet Explorer ("Dim ie As New InternetExplorer") and iterates through the webpage's HTML objects. This version of the project has references to the "Microsoft HTML Object Library" and "Microsoft Internet Controls" libraries and imports the "SHDocVw" and "mshtml" namespaces.
(2) The second version uses a WebClient ("Dim wc As New WebClient") and iterates through the webpage's HTML objects. This version of the project imports the "System.Net" namespace (and does not require any additional libraries other than the default libraries).

With the first approach, I have to monitor the "Busy" and "ReadyState" properties of the Internet Explorer to know when the webpage has finished loading. I work with the webpage's HTML source after creating an HTMLDocument variable ("HTMLDoc = CType(ie.Document, HTMLDocument)"). I quit the Internet Explorer after its work is done ("ie.Quit()").

With the second approach, I use the "DownloadStringAsync" method of the WebClient with a handler to monitor the download progress ("BytesReceived", "TotalBytesToReceive", etc.) and another handler to notify me when the download is complete. I load a WebBrowser object with the downloaded string from the WebClient ("wb.Document.Write(strHTML)") and use the WebBrowser to work with the webpage's HTML source. I dispose of the WebClient and WebBrowser after their work is done ("wc.Dispose()", "wb.Dispose()").

In other applications I have used a WebRequest object and its GetResponse method along with a StreamReader object to collect a webpage's HTML source.

Other than the different project references, namespaces, and functions noted above, I'd like to know what are the guidelines or recommended best practices for when to use a hidden instance of the Internet Explorer vs. a WebClient vs. a WebRequest object for working with the HTML source from a webpage. What would drive a developer to use one approach vs. the other (speed, security, compatibility, ease of use, efficient system resource management, coding "elegance", etc.)? Is one approach technologically more current and the other more obsolete?

I've looked through a lot of the Microsoft on-line documentation and Googled for a couple of days but haven't found much guidance on this issue. I'd appreciate any and all advice from the VBForum experts.

Thanks.

Viewing all articles
Browse latest Browse all 27350

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>