HOME  |    TRAINING  |   FREE TUTORIALS   |   JOBS
Find out more about our new RSS feed.
FREE Tutorial
C# PROGRAMMING: NAMESPACES AND THE BASE CLASSES PART 5 - BROWSING THE INTERNET

CATEGORY
SEARCH OUR OTHER TUTORIALS

DESCRIPTION

Retrieving a file from the Internet really involves two processes: requesting the file, and reading it through a stream.
Click here to be kept informed of our new Tutorials.


This free tutorial is a sample from the book C# Programming with the Public Beta.


We've already covered the latter process - the code to do it is essentially the same as our earlier sample to read text from a file using the StreamReader class. Loading and requesting the file is a little more complicated as it involves several new classes.

We're going to need a couple of classes concerned with web browsing: HttpWebRequest, HttpWebResponse and WebRequestFactory, which are all in the System.Net namespace. The assembly that defines this namespace is not by default loaded in C# projects so we need to add it to our references in the solution explorer. Recall we can do this by right-clicking on the References node in the explorer and selecting Add Reference from the context menu.

Next we add a couple of commands to refer to some new namespaces in the C# source file:

namespace WebRequest
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.WinForms;
using System.Data;
using System.Net;
using System.IO;
using System.Text;

System.Net is for the web classes just mentioned, System.IO is because we will need to use a StreamReader, and System.Text provides a helper enumeration value used in constructing the StreamReader. These latter two classes are both in mscorlib.dll so no new references need to be added at compilation time.

Now we can proceed to the code needed to make our request to the web server and display the results:

HttpWebRequest webreq = 
 (HttpWebRequest)WebRequestFactory.Create
 ("http://localhost/postinfo.html");
HttpWebResponse webresp = 
 (HttpWebResponse)webreq.GetResponse();

StreamReader strm = new StreamReader(
 webresp.GetResponseStream(), Encoding.ASCII);
string sLine;
do
{
 sLine = strm.ReadLine();
 AddItem(sLine);
}
while (sLine != null);
strm.Close();

The request is made through an instance of the class HttpWebRequest. However, it is not possible to construct this instance directly - instead it needs to be constructed by a static method of another class, WebRequestFactory. The WebRequestFactory.Create() method is designed to create a general request for a given URL - in this case we pass the URL of one of the files created on the default web site on my local machine when IIS is installed - postinfo.html. Note that WebRequestFactory.Create() actually returns a reference to a WebRequest object, not an HttpWebRequest object, so we need to cast the return value. HttpWebRequest is derived from WebRequest - the latter class is more general-purpose and able to deal with requests using other protocols besides HTTP.

Once we have the HttpWebRequest instance, we actually make the request by calling its GetResponse() method. GetResponse() returns a WebResponse object, which encapsulates information returned by a web server in response to a request. In a similar manner to WebRequestFactory.Create(), the return value is a reference to a WebResponse rather than an HttpWebResponse, so we need to cast it to the required data type.

Once we have the HttpWebResponse, we simply need to obtain a StreamReader instance that we can use to retrieve the contents of the file. To do this we use a StreamReader constructor that takes two parameters: a more general stream and a value that indicates the encoding type. The stream is obtained from the GetResponseStream() method of the HttpWebResponse class, and the encoding type is Encoding.ASCII, an enumerated value from the System.Text.Encoding class, which indicates that this stream contains ASCII text data.

Although there are a lot of classes involved with this and hence a lot to take in, the actual code is still reasonably short and simple. Running this sample produces this result:

This indicates that the page has been successfully downloaded and displayed.




5 RELATED COURSES AVAILABLE
C# PROGRAMMING
At the end of this course, you will have learned the fundamental skills that are required to design and develop o....
C++ PROGRAMMING
Object oriented programming is fast becoming the leading software design methodology, with C++ becoming ever more....
C PROGRAMMING
This course is design to provide non-C programmers with the essential skills and knowledge necessary to allow the....
C PROGRAMMING IN THE UNIX ENVIRONMENT
This course will provide readers the knowledge to use many of the UNIX 'C' library facilities, interface with the....
MICROSOFT VISUAL C++ 5 AND THE MFC&T 32 BIT WINDOWS PROGRAMMING
A complete 32 bit programming course highlighting the main features regarding the Microsoft Visual C++ programmin....
 
0 RELATED JOBS AVAILABLE
CONTACT US
Friday 25th July 2008  © COPYRIGHT 2008 - VISUALSOFT