15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Utility Classes ❘ 653<br />

System.Object<br />

System.MarshalByRefObject<br />

System.Net.WebRequest<br />

System.Net.WebResponse<br />

System.Net.HttpWebRequest<br />

System.Net.FileWebRequest<br />

System.Net.FtpWebRequest<br />

System.Net.HttpWebResponse<br />

System.Net.FileWebResponse<br />

System.Net.FtpWebResponse<br />

Third-Party Web<br />

Request Classes<br />

figure 24-9<br />

Third-Party Web<br />

Response Classes<br />

The fact that you need an object specifically capable of dealing with the HTTP protocol is clear from the<br />

URI that you supply to WebRequest.Create(). WebRequest.Create() examines the protocol specifier in<br />

the URI to instantiate <strong>and</strong> return an object of the appropriate class. This keeps your code free from having<br />

to know anything about the derived classes or specific protocol used. When you need to access specific<br />

features of a protocol, you might need the properties <strong>and</strong> methods of the derived class, in which case you<br />

can cast your WebRequest or WebResponse reference to the derived class.<br />

With this architecture, you should be able to send requests using any of the common protocols. However,<br />

Microsoft currently provides derived classes to cover only the HTTP, HTTPS, FTP, <strong>and</strong> FILE protocols.<br />

The FTP option is the latest option provided by the .<strong>NET</strong> Framework (ever since the release of the .<strong>NET</strong><br />

Framework 2.0). If you want to utilize other protocols, for example SMTP, then you need to use the<br />

Windows Communication Foundation, revert to using the Windows API, or use the SmtpClient object.<br />

uTiliTy Classes<br />

This section covers a couple of utility classes to make web programming easier when dealing with URIs <strong>and</strong><br />

IP addresses.<br />

uris<br />

Uri <strong>and</strong> UriBuilder are two classes in the System (not System.Net) namespace, <strong>and</strong> they are both<br />

intended to represent a URI. UriBuilder allows you to build a URI given the strings for the component<br />

parts, <strong>and</strong> the Uri class allows you to parse, combine, <strong>and</strong> compare URIs.<br />

For the Uri class, the constructor requires a completed URI string:<br />

Uri MSPage = new<br />

Uri("http://www.Microsoft.com/SomeFolder/SomeFile.htmOrder=true");<br />

The class exposes a large number of read-only properties. A Uri object is not intended to be modified after<br />

it has been constructed:<br />

string Query = MSPage.Query;<br />

string AbsolutePath = MSPage.AbsolutePath;<br />

string Scheme = MSPage.Scheme;<br />

int Port = MSPage.Port;<br />

string Host = MSPage.Host;<br />

bool IsDefaultPort = MSPage.IsDefaultPort;<br />

// Order=true;<br />

// /SomeFolder/SomeFile.htm<br />

// http<br />

// 80 (the default for http)<br />

// www.microsoft.com<br />

// true since 80 is default<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!