15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

640 ❘ ChaPTer 24 netwOrkinG<br />

Although the WebClient class is very simple to use, it has very limited features. In particular, you cannot<br />

use it to supply authentication credentials — a particular problem with uploading data is that not many sites<br />

will accept uploaded files without authentication! It is possible to add header information to requests <strong>and</strong> to<br />

examine any headers in the response, but only in a very generic sense — there is no specific support for any<br />

one protocol. This is because WebClient is a very general-purpose class designed to work with any protocol<br />

for sending a request <strong>and</strong> receiving a response (such as HTTP or FTP). It cannot h<strong>and</strong>le any features specific<br />

to any one protocol, such as cookies, which are specific to HTTP. To take advantage of these features, you<br />

need to use a family of classes based on two other classes in the System.Net namespace: WebRequest <strong>and</strong><br />

WebResponse.<br />

WebrequesT <strong>and</strong> WebresPonse Classes<br />

The WebRequest class represents the request for information to send to a particular URI. The URI is passed<br />

as a parameter to the Create() method. A WebResponse represents the data you retrieve from the server.<br />

By calling the WebRequest.GetResponse() method, you actually send the request to the web server <strong>and</strong><br />

create a WebResponse object to examine the return data. As with the WebClient object, you can obtain a<br />

stream to represent the data, but in this case you use the WebResponse.GetResponseStream() method.<br />

This section briefly discusses a few of the other areas supported by WebRequest, WebResponse, <strong>and</strong> other<br />

related classes.<br />

You start off by downloading a web page using these classes, which is the same example as before, but using<br />

WebRequest <strong>and</strong> WebResponse. In the process, you uncover the class hierarchy involved, <strong>and</strong> then see how<br />

to take advantage of extra HTTP features that are supported by this hierarchy.<br />

The following code shows the modifications you need to make to the BasicWebClient sample to use the<br />

WebRequest <strong>and</strong> WebResponse classes:<br />

public Form1()<br />

{<br />

InitializeComponent();<br />

}<br />

WebRequest wrq = WebRequest.Create("http://www.reuters.com");<br />

WebResponse wrs = wrq.GetResponse();<br />

Stream strm = wrs.GetResponseStream();<br />

StreamReader sr = new StreamReader(strm);<br />

string line;<br />

while ( (line = sr.ReadLine()) != null)<br />

{<br />

listBox1.Items.Add(line);<br />

}<br />

strm.Close();<br />

In the code example, you start by instantiating an object representing a web request. You don’t do this using<br />

a constructor, but instead call the static method WebRequest.Create(). As you learn in more detail later<br />

in this chapter (see “The Web Request <strong>and</strong> Web Response Hierarchy” section), the WebRequest class is part<br />

of a hierarchy of classes supporting different network protocols. To receive a reference to the correct object<br />

for the request type, a factory mechanism is in place. The WebRequest.Create() method will create the<br />

appropriate object for the given protocol.<br />

An important part of the HTTP protocol is the ability to send extensive header information with both<br />

request <strong>and</strong> response streams. This information can include cookies <strong>and</strong> the details of the particular<br />

browser sending the request (the user agent). As you would expect, the .<strong>NET</strong> Framework provides full<br />

support for accessing the most significant data. The WebRequest <strong>and</strong> WebResponse classes provide some<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!