Download a Web Page using the WebClient - Create a form using the WebClient Control
(Page 4 of 4 )
Another way of opening an URL to read its contents is to use the WebClient Control, which may be added to the Toolbox using the same procedure used for adding the WebBrowser control. Add a new form, WebClientControl.vb, to the project. From the Toolbox double click the WebClient control when the form is in the design view. Add a command button and a textbox whose multi-line property is set to true, with both scroll bars showing. The WebClient control gets to the component tray area as shown.

To the click event of the command button add code as shown:
Imports System.IO
Public Class WebClientControl
Private Sub Button1_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
Dim wc As New System.Net.WebClient
Dim strm As New StreamReader _
(wc.OpenRead("http://localhost/TestXMLHttp/TestClient.htm"))
Dim s As String = strm.ReadToEnd
TextBox1.Text = s
End Sub
End Class
This time the OpenRead() method reads the TestClient.htm file on the local intranet. You are not referencing the class directly by the imports statement, rather you are instantiating a New WebClient. Otherwise using the StreamReader is similar to the previous case. When you run this form, your display appears as shown.

Summary
WebClient Class and the WebClient Control are very useful for interacting with web sites. The many properties and methods can be put to good use for importing any part of a web site. By creating a COM wrapper the controls can be called from VB 6. However the Inet and WebBrowser controls can continue to be used in VB6 programs. What is achieved using several lines of code in the Visual Basic 2005 Express Edition program can be achieved by two lines in a VB6 project, however one is object-oriented and the other is object-based. Whether Microsoft will continue to support VB 6 or not is another point to consider.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |