..


Sponsored Links

Developing applications that access resources online

Article written by Vincent Gaglio
Page 1 of 5

In most cases, access to resources on the Internet through browsers such as Internet Explorer or Mozilla Firefox. However many times you may need to add functionality or access Web content management applications within Windows.

The aim of this paper is to analyze the classes. NET Framework applications that allow you to perform these operations. In particular the namespace more interesting in this regard is the System.Net, which allows high-level operations, such as downloading and uploading files or making web requests through different protocols.

Class System.Net.WebClient

If you simply want to request a file from a particular URL is the class to use System.Net.WebClient. It is a simple class to use and designed to perform simple operations using one or two commands.

To download a file the WebClient class provides two methods: DownloadFile () and OpenRead (). The method to use depends on how you want to process the contents of the file: if you just want to save the file to disk should use the first, which takes as parameters the address from which to download the file and the location to save (including file name), and if you want to operate on downloaded data must use the second method, which requires only the address as a parameter and returns a reference file of type Stream through which to access data.

The syntax of these two methods is as follows






 WebClient client = new WebClient ();







 Client.DownloadFile ("http://www.virgilio.it/", "home.htm");









 WebClient client = new WebClient ();







 Stream strm = Client.OpenRead ("http://www. Virgilio.it /");



Let's see an example of using the method OpenRead (). Suppose you want to display in a ListBox control the content of a web page downloaded from the internet. To do this we create a new Windows Forms project in Visual Studio and the main form of the same insert a ListBox named listBox1.

At the beginning of the file. Cs add directives System.Net and System.IO and then in the Load event of the form insert the following code






 WebClient client = new WebClient ();









 Stream strm = Client.OpenRead ("http://www.virgilio.it");







 StreamReader sr = new StreamReader (strm);







 string line;









 while ((line = sr.ReadLine ())! = null)







 {



      



 ListBox1.Items.Add (line);







 }







 strm.Close ();



In this example we use the StreamReader class to obtain a stream of data from the network which then can be managed with methods like ReadLine ().

Running the application, the result should be similar to the following

that displays the contents of the web page.

In the same category ...
E-Learning
AutoCAD (eBook) AutoCAD (eBook)
Creation of architectural structures. At only 29 €.
Java (Course) Java (Course)
OOP Programming in Java SUN. -15% Discount until 12/10/2011.
Linux (Course) Linux (Course)
Complete guide to open-source system. From 49 €.
Sponsored Links