WebClient


Let's start with the class that is easiest to use: the WebClient class. This class is a component that can be dragged and dropped from the Toolbox to a Windows Forms application. You can use it with client applications to access HTTP or FTP servers or use it to access files in the file system.

The following table shows the major properties and methods of the WebClient class.

WebClient Properties

Description

BaseAddress

The property BaseAddress defines the base address of for a request that is made by the WebClient.

Credentials

With the Credentials property you can pass authentication information, for example by using the NetworkCredential class.

UseDefaultCredentials

If the credentials of the currently logged on user should be used, set the property UseDefaultCredentials to true.

Encoding

The Encoding property can be used to set the encoding of strings to upload and download.

Headers

With the Headers property you can define a WebHeaderCollection that can contain header information specifically for the protocol that is used.

Proxy

By default the proxy that is configured with Internet Explorer is used to access the Internet. If a different proxy is needed, you can define a WebProxy object with the Proxy property.

QueryString

If a query string needs to be sent to the Web server, you can do this by setting a NameValueCollection with the QueryString property.

ResponseHeaders

After sending the request, you can read the header information of the response within the WebHeaderCollection class that is associated with the ResponseHeaders class.

The WebClient class has simple methods to upload and download files, which are listed in the following table.

WebClient Methods

Description

DownloadData()

With the DownloadData() method, you can pass a string for the Web address that is appended to the BaseAddress, and a byte array containing the data from the server is returned.

DownloadString()

DownloadString() is similar to DownloadData(); the only difference is that the returned data is stored inside a string.

DownloadFile()

If the data that is returned from the server should be stored inside a file, the method DownloadFile() does all the work for you. The arguments to set with this method are the Web address and the filename.

OpenRead()

With the OpenRead() file you also download data from the server, but the data downloaded can be read from a stream returned by this method.

UploadFile()

To upload files to an FTP or Web server, the method UploadFile() can be used. This method also allows passing the HTTP method that should be used with the upload.

UploadData()

While UploadFile() allows uploading files from the local file system, UploadData() sends a byte array to the server.

UploadString()

With UploadString(), you can upload a string to the server.

UploadValues()

With UploadValues(), you can upload a NameValueCollection to the server.

OpenWrite()

The method OpenWrite() sends content to a server by using a stream.

In the following Try It Out, you download a file from the Web with the WebClient class.

Try It Out – Using the WebClient Class

image from book
  1. Create a new Console Application Project named WebClientDemo in the directory C:\ BegVCSharp\Chapter29.

  2. Import the namespace System.Net.

  3. Add the following code to the Main() method.

    static void Main(string[] args) { WebClient client = new WebClient(); client.BaseAddress = "http://www.microsoft.com"; string data = client.DownloadString("Office"); Console.WriteLine(data); }
  4. Compile and run the application.

How It Works

After instantiating the WebClient class, the property BaseAddress is set to http://www.microsoft.com. This is the left part of the address that is used with all the following requests. The method DownloadString("Office") requests the page http://www.microsoft.com/Office. All the returned data is stored in the variable data and written to the console.

image from book




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

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