System.Net Classes-Overview
In the System.Net namespace we have networking classes for IP address lookups, network authentication, permissions, and classes for sending and receiving data.
Let us look at these classes by sorting them into some groups.
Name Lookup
To get an IP address from a DNS hostname, or to get a hostname from an IP address the Dns class can be used. The DnsPermission class represents the permission required for name lookups. DnsPermissionAttribute is an attribute class to mark assemblies, classes, or methods that need this privilege.
IP Addresses
IP addresses are handled within the class IPAddress. A single host can have multiple IP addresses and alias names. All this information is contained within the class IPHostEntry. The Dns class returns an object of type IPHostEntry when we do a name lookup.
Authentication and Authorization
The AuthenticationManager class has static methods to authenticate the client user. This utility class uses modules that implement the IAuthenticationModule interface. The AuthenticationManager asks these modules to authenticate the user. The authentication modules get request information and the user credentials with the ICredentials interface, and return an Authorization object for authorized users that are allowed to use the resource.
The client application can pass credentials to the server with an instance of the NetworkCredential class. User credentials can be cached in the CredentialCache.
In this chapter we will cover an overview of the authentication and authorization mechanism, but we will look into it in detail in .
Requests and Responses
The abstract base classes for sending requests to a server and receiving a response are WebRequest and WebResponse. In the System.Net namespace we have some specific implementations of these classes for HTTP and file access: HttpWebRequest, HttpWebResponse, FileWebRequest, and FileWebResponse.
The HttpXXX classes also make use of another class in the System.Net namespace: the HttpVersion class is used to specify the HTTP version. HttpWebRequest and HttpWebResponse classes both have a ProtocolVersion property that defines the HTTP version-HttpVersion.Version10 or HttpVersion.Version11. HTTP 1.0 was used in the early days of the Internet and is still used by some web servers, HTTP 1.1, the current version, has some more features such as keeping a connection open for multiple requests.
In this chapter we will discuss the functionality of the WebRequest and WebResponse classes, and the specific implementations FileWebRequest and FileWebResponse. The HTTP protocol and the related classes will be discussed in .
The permissions needed with the request and response classes are defined with the WebPermission class and the attribute class WebPermissionAttribute.
A component class that makes it easy to use WebRequest and WebResponse from the Visual Studio .NET designer is the WebClient class. This class derives from the Component class and so it can be used with drag and drop functionality from the Toolbox. However, it is not configured to the Toolbox by default. With the WebClient class it is easy to download files from and upload files to a server.
Connection Management
The ServicePoint and ServicePointManager classes play an important role for HTTP connections. An instance of the ServicePoint class is associated with a URI to a resource and can handle multiple connections. The utility class ServicePointManager manages ServicePoint objects by creating new objects or finding existing ones.
We can increase the throughput of an application that requests a lot of data from a server simultaneously by increasing the number of connections on an application base. By default, the maximum number of connections to the same server is two to limit network resources needed. Later in this chapter in the Connection Pooling section we will look at where the default value is specified, and how it can be changed.
Creating connection pools is useful for middle-tier applications that connect to Internet resources on behalf of a specific user. We can reuse connections in connection groups where each group is associated with user credentials.
Cookies
Cookies are sets of data, stored on the client-side, and used by the server to remember some information between requests. When using a web browser like Internet Explorer to request some data from a web server, Internet Explorer itself manages the acceptance and storage of cookies, and sends the cookie back to the web server. If we create a custom application that requests data from a web server that sends cookies, we can read these in an object of the class CookieCollection that is returned from the Cookies property of an HttpWebResponse. We can pass cookies to the server with the help of the CookieContainer class. A cookie itself is represented in the Cookie class.
Cookies are sent within the header of the HTTP protocol, so cookies will be covered again when we look at the HTTP protocol in .
Proxy Server
A proxy server is used in a network environment to direct a connection to the Internet through a single system (or multiple systems depending on the network size). The proxy server can cache pages that are requested by users, so if the same page is requested several times a request to the web server is not needed anymore because the web proxy can answer the request itself.
The WebProxy class is used to define the proxy server that should be consulted for Internet requests. With the GlobalProxySelection class we can define a default proxy server that should be used for all requests if not specified otherwise for a specific request.
Sockets
Instead of using the web classes we can get more features and flexibility, but also more complexity with socket classes. Most of the classes that are used with socket programming can be found in the namespace System.Net.Sockets.
With socket programming not only can we do connection-oriented programming as it is used with HTTP, we can also do connection-less programming as is used with UDP broadcasts and multicasts. Socket programming is extremely flexible and allows the use of different protocols such as GGP, ICMP, IGMP, IPX, and SPX.
In the following chapters, we discuss socket programming with both the TCP and the UDP protocol, with connection-oriented data transfer and connection-less data transfer for broadcasts and multicasts.
Now that we have an overview of the most important classes in the System.Net namespace, let's take a more detailed look at some of these classes, as they are the foundation for the rest of the book.