is a large namespace that contains many members. It is far beyond the scope of
this chapter to discuss them all or to discuss all aspects related to Internet programming. (In
fact, an entire book is needed to fully cover networking and C#’s support for it in detail.)
822
P a r t I I :
E x p l o r i n g t h e C # L i b r a r y
The classes defined by
System.Net
are shown here:
AuthenticationManager
Authorization
Cookie
CookieCollection
CookieContainer
CookieException
CredentialCache
Dns
DnsPermission
DnsPermissionAttribute
DownloadDataCompletedEventArgs
DownloadProgressChangedEventArgs
DownloadStringCompletedEventArgs
EndPoint
EndpointPermission
FileWebRequest
FileWebResponse
FtpWebRequest
FtpWebResponse
HttpListener
HttpListenerBasicIdentity
HttpListenerContext
HttpListenerException
HttpListenerPrefixCollection
HttpListenerRequest
HttpListenerResponse
HttpVersion
HttpWebRequest
HttpWebResponse
IPAddress
IPEndPoint
IPEndPointCollection
IPHostEntr y
IrDAEndPoint
NetworkCredential
OpenReadCompletedEventArgs
OpenWriteCompletedEventArgs
ProtocolViolationException
Ser vicePoint
Ser vicePointManager
SocketAddress
SocketPermission
SocketPermissionAttribute
UploadDataCompletedEventArgs
UploadFileCompletedEventArgs
UploadProgressChangedEventArgs
UploadStringCompletedEventArgs
UploadValuesCompletedEventArgs
WebClient
WebException
WebHeaderCollection
WebPermission
WebPermissionAttribute
WebProxy
WebRequest
WebRequestMethods
WebRequestMethods.File
WebRequestMethods.Ftp
WebRequestMethods.Http WebResponse
System.Net
defines the following interfaces:
IAuthenticationModule
ICer tificatePolicy
ICredentialPolicy
ICredentials
ICredentialsByHost
IWebProxy
IWebProxyScript
IWebRequestCreate
www.freepdf-books.com
PART II
C h a p t e r 2 5 :
N e t w o r k i n g T h r o u g h t h e I n t e r n e t U s i n g S y s t e m . N e t
823
It defines these enumerations:
AuthenticationSchemes
DecompressionMethods
FtpStatusCode
HttpRequestHeader
HttpResponseHeader
HttpStatusCode
NetworkAccess
SecurityProtocolType
Transpor tType
WebExceptionStatus
System.Net
also defines several delegates.
Although
System.Net
defines many members, only a few are needed to accomplish most
common Internet programming tasks. At the core of networking are the abstract classes
WebRequest
and
WebResponse
. These classes are inherited by classes that support a
specific network protocol. (A
protocol
defines the rules used to send information over a
network.) For example, the derived classes that support the standard HTTP protocol are
HttpWebRequest
and
HttpWebResponse
.
Even though
WebRequest
and
WebResponse
are easy to use, for some tasks you can
employ an even simpler approach based on
WebClient
. For example, if you only need to
upload or download a file, then
WebClient
is often the best way to accomplish it.
Uniform Resource Identifiers
Fundamental to Internet programming is the Uniform Resource Identifier (URI). A
URI
describes the location of some resource on the network. A URI is also commonly called a
URL,
which is short for
Uniform Resource Locator.
Because Microsoft uses the term
URI
when
describing the members of
System.Net
, this book will do so, too. You are no doubt familiar
with URIs because you use one every time you enter an address into your Internet browser.
A URI has the following general form:
Protocol:
//
ServerID
/
FilePath
?
Query
Protocol
specifies the protocol being used, such as HTTP.
ServerID
identifies the specific
server, such as mhprofessional.com or HerbSchildt.com.
FilePath
specifies the path to a
specific file. If
FilePath
is not specified, the default page at the specified
ServerID
is obtained.
Finally,
Query
specifies information that will be sent to the server.
Query
is optional. In C#,
URIs are encapsulated by the
Uri
class, which is examined later in this chapter.
Internet Access Fundamentals
The classes contained in
System.Net
support a request/response model of Internet interaction.
In this approach, your program, which is the client, requests information from the server
and then waits for the response. For example, as a request, your program might send to the
server the URI of some website. The response that you will receive is the hypertext associated
with that URI. This request/response approach is both convenient and simple to use
because most of the details are handled for you.
The hierarchy of classes topped by
WebRequest
and
WebResponse
implement what
Microsoft calls
pluggable protocols.
As most readers know, there are several different types of
network communication protocols. The most common for Internet use is HyperText Transfer
Protocol (HTTP). Another is File Transfer Protocol (FTP). When a URI is constructed, the
www.freepdf-books.com
824
P a r t I I :
E x p l o r i n g t h e C # L i b r a r y
prefix of the URI specifies the protocol. For example, http://www.HerbSchildt.com uses the
prefix
http,
which specifies hypertext transfer protocol.
As mentioned earlier,
WebRequest
and
WebResponse
are abstract classes that define the
general request/response operations that are common to all protocols. From them are derived
concrete classes that implement specific protocols. Derived classes register themselves, using
the static method
RegisterPrefix( )
, which is defined by
WebRequest
. When you create a
WebRequest
object, the protocol specified by the URI’s prefix will automatically be used, if it
is available. The advantage of this “pluggable” approach is that most of your code remains the
same no matter what type of protocol you are using.
The .NET runtime automatically defines the HTTP, HTTPS, file, and FTP protocols.
Thus, if you specify a URI that uses the HTTP prefix, you will automatically receive the
HTTP-compatible class that supports it. If you specify a URI that uses the FTP prefix, you
will automatically receive the FTP-compatible class that supports it.
Because HTTP is the most commonly used protocol, it is the only one discussed in this
chapter. (The same techniques, however, will apply to all supported protocols.) The classes
that support HTTP are
HttpWebRequest
and
HttpWebResponse
. These classes inherit
WebRequest
and
WebResponse
and add several members of their own, which apply to
the HTTP protocol.
System.Net
supports both synchronous and asynchronous communication. For many
Internet uses, synchronous transactions are the best choice because they are easy to use.
With synchronous communications, your program sends a request and then waits until
the response is received. For some types of high-performance applications, asynchronous
communication is better. Using the asynchronous approach, your program can continue
processing while waiting for information to be transferred. However, asynchronous
communications are more difficult to implement. Furthermore, not all programs benefit
from an asynchronous approach. For example, often when information is needed from
the Internet, there is nothing to do until the information is received. In cases like this, the
potential gains from the asynchronous approach are not realized. Because synchronous
Internet access is both easier to use and more universally applicable, it is the only type
examined in this chapter.
Since
WebRequest
and
WebResponse
are at the heart of
System.Net
, they will be
examined next.
WebRequest
The
WebRequest
class manages a network request. It is abstract because it does not
implement a specific protocol. It does, however, define those methods and properties
common to all requests. The methods defined by
WebRequest
that support synchronous
communications are shown in Table 25-1. The properties defined by
WebRequest
are shown
in Table 25-2. The default values for the properties are determined by derived classes.
WebRequest
defines no public constructors.
To send a request to a URI, you must first create an object of a class derived from
WebRequest
that implements the desired protocol. This is done by calling
Create( )
, which
is a
static
method defined by
WebRequest
.
Create( )
returns an object of a class that inherits
WebRequest
and implements a specific protocol.
www.freepdf-books.com