Download a Web Page using the WebClient - Creating a Windows project to use the WebClient Class
(Page 2 of 4 )
Create a Windows Application project by accessing the New Project from the File menu item. This opens the New project window. Choose the Windows Application as shown and change the default WindowsApplication1 name to something different. In the present case it is called ExploreWebClient.

Change the default Form1.vb to WebPage.vb. Using the WebClient class we will download a web page, in this case, a blog from the site http://hodentek.blogspot.com/. This is a blog that requires no authentication; you may use any other web page as well. To the WebPage.vb form add a button, a textbox, and a WebBrowser control. If the WebBrowser control is not already in the Toolbox, you may add the control by going to Toolbox--> Choose Toolbox items...

and click the hyperlink to open the Choose ToolBox Items Window as shown.

This adds the control to the Toolbox and appears immediately when you click OK to the above after placing a check mark against the item.
With these controls added, your page should appear as shown in the next picture. The area above the button is the textbox which has been set in design to show multi-lines and to show both scroll bars. It does not show the horizontal scroll bar for reasons which are not clear. The clear area below the button is the resized WebBrowser control.

To the click event of the button add the following code as shown:
Imports System.Net
Imports System.IO
Public Class WebPage
Dim wclient As New WebClient
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim sr As New StreamReader(wclient. _
OpenRead("http://hodentek.blogspot.com/"))
Dim s As String = sr.ReadToEnd
TextBox1.Text = s
WebBrowser1.Navigate("http://hodentek.blogspot.com/")
End Sub
End Class
The Imports System.NET allows us to use the WebClient class and spawn a new instance by calling the New method. The newly created instance can call one of its methods, OpenRead() to read the contents of the URL.
This gets directly read into the Stream by the StreamReader, and all that is read is sent to the textbox. Since you are using the StreamReader, you need the second imports statement, System.IO.
The WebBrowser1 ActiveX control is straightforward to code. Given an URL, it displays the web page.
Next: Know your options >>
More Code Examples Articles
More By Jayaram Krishnaswamy