Interacting with Web Resources

Team Fly 

Page 308

Interacting with Web Resources

Another group of classes in the System.Net namespace handles the interaction with web resources. The WebClient class provides the functionality needed by a Windows application to interact with a web server: retrieve HTML pages or files and upload files to the server. The WebClient class is very simple and supports synchronous operations only. However, it abstracts the details of accessing a web server and makes the process of exchanging files with the web server as simple as reading from, or writing to, a local file. The WebClient class provides methods for exchanging information with a web server through streams, similar to accessing local files.

The WebClient class is part of the System.Net namespace, which you must import to your application. Then you can create instances of the WebClient class and call its methods. A WebClient object need not establish a connection to the web server explicitly; you just specify the desired URL when you request a document, or when you want to upload a document from the local computer. Because of this, using the WebClient class is almost trivial, and its functionality is exposed through a small number of methods, discussed next:

DownloadData method The DownloadData method downloads data from a web server and returns them in an array of bytes. The syntax of the method is:

      WebClient.DownloadData(documentURL) 

where documentURL is the URI of the document to download. If you're downloading an existing file, you specify the URI of this file. You can also specify the URI of an ASP application that generates its output on the fly. The output of the script is transmitted to the client and you can retrieve it as an array of bytes. The following statements will download the main page of the Sybex site and store the HTML document in an array of bytes:

 Dim wClient As New WebClient Dim bytes() As Byte bytes = wClient.DownloadFile(''www.sybex.com") 

To convert the byte array to a string, use the members of the System.Text.Encoding class. The following statement will display the HTML code of this page on a message box:

 MsgBox(System.Text.Encoding.UTF8.GetString(bytes)) 

DownloadFile method The DownloadFile method is similar to the DownloadData method, but a little more convenient, because it allows you to specify the path of the file where the data will be stored at the client. The syntax of the DownloadFile method is:

 WebClient.DownloadFile(documentURL, localFileName) 

The first argument is the document's URL and the second argument is the path of the local file where the downloaded data will be stored. The following method will download the main page of the Sybex site and store it to the specified local file:

 Dim wClient As New WebClient wClient.DownloadFile("www.sybex.com", "C:\DLoads\Data\Sybex.htm") 
Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net