The WebClient Class

I l @ ve RuBoard

The WebClient Class

Why, you might ask, are there two completely different sets of classes for making "Web" requests in the .NET Framework? We've already covered the WebRequest / WebResponse architecture. What value does the WebClient class add? For one, it provides a simple, single object model for making similar requests. In fact, like the WebRequest class, the WebClient class processes generic URI requests. There's a reason for this. The WebClient class is implemented using the WebRequest and WebResponse classes ”it just hides them. For simple scenarios, the WebClient class might just fit the bill.

Warning

The WebClient class does not provide any support for asynchronous operations.


To get an idea of what the WebClient class can do for you, check out the following code example. It performs the same function as our original WebRequest example, but it uses less code.

 ImportsSystem ImportsSystem.Net ImportsSystem.Text ImportsSystem.IO ModuleWebClientTest SubMain() DimclientAsWebClient=NewWebClient() DimresStreamAsStream=_ client.OpenRead("http://www.gotdotnet.com/about_new.aspx") DimsrAsStreamReader=NewStreamReader(resStream) DimresChar(256)AsChar DimcharCountAsInteger=sr.Read(resChar,0,resChar.Length) DoWhilecharCount>0 DimstrAsString=NewString(resChar,0,charCount) Console.Write(str) charCount=sr.Read(resChar,0,resChar.Length) Loop resStream.Close() EndSub EndModule 

As I mentioned, internally the WebClient class uses both the WebRequest and WebResponse classes. The WebClient exposes methods both for sending and reading data. However, because it is designed to keep things simple, it provides support for synchronous (blocking) calls only. Table 6-1 describes the major methods of interest for the WebClient class.

Table 6-1. Selected Methods of the WebClient Class

Method

Description

DownloadData

Downloads a byte array from the server

DownloadFile

Downloads data from an URI to a local file

OpenRead / OpenWrite

Opens a stream from an URI for reading/writing

UploadData / UploadFile

Uploads data or local file to an URI

UploadValues

Uploads name /value pairs to an URI

Apart from the fact that all WebClient method calls are synchronous, other potential disadvantages include:

  • You can't maintain cookies between the calls. You must use the HttpWebRequest object's CookieContainer property.

  • ResponseHeaders contains name/value header pairs, but there is no way to catch response code unless there is an error. In case of an error, a WebException will be thrown and its response property will contain the HttpWebResponse that was returned in error; there you can see the StatusCode . If you need to check nonerror status codes, you have to use the HttpWebRequest class.

  • You can't know the download status (the amount of data that has been processed and copied ). You must instead use the WebRequest / WebResponse objects.

When you deal with downloads in general, and WebClient in particular, you should be aware of the classes described in Table 6-2. These classes can help your application work through proxy servers and handle authentication and credentials.

Table 6-2. Useful Classes for Making Web Requests

Class

Description

WebProxy

If the request goes through a proxy, you must specify this.

Credentials

This is a property of WebProxy and WebClient that you must specify with the NetworkCredential or CredentialCache of the proxy and the server, respectively.

GlobalProxySelection

This contains the default proxy settings for all HTTP requests. Assign the WebProxy created to this Global ­ProxySelection .

Here's a sample that uses some of the other useful methods of the WebClient class. Note the use of the DownloadData and DownloadFile methods.

 ImportsSystem ImportsSystem.Net ImportsSystem.Text ImportsSystem.Collections.Specialized ModuleWebClientSample PublicSubMain() Try DimclientAsNewWebClient() DimpageDataAsByte()=_ client.DownloadData("http://www.gotdotnet.com") DimpageHtmlAsString=Encoding.ASCII.GetString(pageData) Console.WriteLine(pageHtml) client.DownloadFile("http://www.gotdotnet.com", "C:\Temp.html") CatchwebExAsWebException Console.WriteLine(webEx.ToString()) EndTry Console.WriteLine("Hitanykeytocontinue...") Console.Read() EndSub EndModule 

Most of the information about the WebClient is pretty much a rehash of the WebRequest material, so I won't spend any further time on it. The MSDN documentation is quite helpful on this topic, and the information contained here should be more than enough to get you up and running.

I l @ ve RuBoard


Designing Enterprise Applications with Microsoft Visual Basic .NET
Designing Enterprise Applications with Microsoft Visual Basic .NET (Pro-Developer)
ISBN: 073561721X
EAN: 2147483647
Year: 2002
Pages: 103

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