The WebClient Class

 
Chapter 20 - Accessing the Internet
bySimon Robinsonet al.
Wrox Press 2002
  

If you only want to request a file from a particular URI, then you will find that the easiest .NET class to use is System.Net.WebClient . This class is an extremely high-level class designed to perform basic operations with only one or two commands. The .NET Framework currently supports URIs beginning with http: , https : , and file: identifiers.

It is worth noting that the term URL (Uniform Resource Locator) is no longer in use in new technical specifications, and URI ( Uniform Resource Identifier ) is now preferred. URI has roughly the same meaning as URL, but is a bit more general since URI does not imply we are using one of the familiar protocols, such as HTTP or FTP.

Downloading Files

There are two methods available for downloading a file using WebClient . The method we choose depends on how we want to process the file contents. If we simply want to save the file to disk we use the DownloadFile() method. This method takes two parameters: the URI of the file, and a location ( path and filename) to save the requested data.

   WebClient Client = new WebClient();     Client.DownloadFile("http://www.Wrox.com/index.asp", "index.htm");   

More commonly, your application will want to process the data retrieved from the web site. In order to do this you use the OpenRead() method. OpenRead() returns a Stream reference you can then use to retrieve the data into memory.

   WebClient Client = new WebClient();     Stream strm = Client.OpenRead("http://www.Wrox.com/default.asp");   

Basic Web Client Example

Our first example will demonstrate the WebClient.OpenRead() method. We will display the contents of the downloaded page in a ListBox control. We create the project as a standard C# Windows application, add a ListBox called listBox1 with the docking property set to DockStyle.Fill . At the beginning of the file, we will need to add the System.Net and System.IO namespaces to our list of using directives. We then make the following changes to the constructor of the main form

 public Form1() {    InitializeComponent();   System.Net.WebClient Client = new WebClient();     Stream strm = Client.OpenRead("http://www.wrox.com");     StreamReader sr = new StreamReader(strm);     string line;     while ( (line=sr.ReadLine()) != null )     {     listBox1.Items.Add(line);     }         strm.Close();   } 

In this example, we connect a StreamReader from the System.IO namespace to the network stream. This allows us to obtain data from the stream as text through the use of higher-level methods, such as ReadLine() . This is an excellent example of the point made in Chapter 12 about the benefits of abstracting data movement into the concept of a stream.

Running this sample produces the following results:

click to expand

There is also an OpenWrite() method in the WebClient class. This method returns a writeable stream for you to send data to a URI. You can also specify the method used to send the data to the host; the default method is POST. The following code snippet assumes a writeable directory named accept on the local machine. The code will create a file in the directory with the name newfile.txt and the contents " Hello World ".

   WebClient webClient = new WebClient();     Stream stream = webClient.OpenWrite("http://localhost/accept/newfile.txt",     "PUT");     StreamWriter streamWriter = new StreamWriter(stream);     streamWriter.WriteLine("Hello World");     streamWriter.Close();   

Uploading Files

The WebClient class also features UploadFile() and UploadData() methods. UploadFile() uploads a file to a specified location given the local filename, while UploadData() uploads binary data supplied as an array of bytes to the specified URI (there is also a DownloadData() method for retrieving an array of bytes from a URI).

   WebClient client = new WebClient();     client.UploadFile("http://www.ourwebsite.com/NewFile.htm",     "C:\WebSiteFiles\NewFile.htm");     byte [] image;     // code to initialise image so it contains all the binary data for     // some jpg file     client.UploadData("http://www.ourwebsite.com/NewFile.jpg", image);   
  


Professional C#. 2nd Edition
Performance Consulting: A Practical Guide for HR and Learning Professionals
ISBN: 1576754359
EAN: 2147483647
Year: 2002
Pages: 244

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