Using a Web Proxy
In a local network a proxy server can be used to route Internet access to specific servers. A proxy server can reduce the transfer and network connections from the Internet, and increase the performance for local clients by caching resources.
The proxy server can do active and passive caching.
With passive caching the web resources are stored in the cache of the proxy server as soon as a client requests a resource. If a second client requests the same resource, it is not necessary to get the resource from the web server in the Internet again as the web proxy can answer directly from the cache created by the first request.
Using active caching the system administrator can configure specific web servers and directories that should be cached automatically following a specific schedule, such as during night hours. This way the network bandwidth needed for the Internet can be reduced during the daytime, and users will see a better performance for often used pages.
The default proxy server is set with the Internet Options in the Control Panel. You can also access this configuration from the Internet Explorer Tools | Internet Options | Connections | LAN Settings:
Here the web proxy server has the IP address 172.31.24.21 and listens to port 80. For web servers in an intranet, the proxy server should not be used. This is marked with the option Bypass proxy server for local addresses. With the Advanced…button it is possible to configure different proxy servers for different protocols (such as HTTP, HTTPS, or FTP), and it is possible to select specific web sites that should not be accessed by the proxy server.
WebProxy class
The WebProxy class is used to define a proxy server. This class has properties that are similar to the settings we have seen with the proxy server configuration:
WebProxy Properties
Description
Address
The Address is of type Uri and defines the URI to the proxy server, IP address, or name and port number.
BypassList
With the property BypassList, a string array can get and set URIs that should not use the proxy server.
BypassArrayList
BypassArrayList is a read-only property that returns an object of type ArrayList which represents the URIs that we set with the property BypassList.
BypassProxyOnLocal
BypassProxyOnLocal is a Boolean property that specifies if local addresses should be used with the proxy server or not.
Credentials
If the proxy server requires user authentication, we can pass the user credentials with the Credentials property.
Default Web Proxy
The default proxy server that is used for all connections is set with the GlobalProxySelection class. By default, the web proxy that is set with the Internet Options we have just seen is used. With the Select property we can set a different proxy for all uses of WebRequest.GetResponse().
In the program below we access the information from the default web proxy and write it to the console. The Select property of the GlobalProxySelection class returns an IWebProxy interface. To access the properties of the WebProxy class we have to cast this interface to the WebProxy class. Then we access the URI of the proxy server that is defined with the Address property. The BypassList property returns a string array that we write out inside the foreach loop. Finally, we use the BypassProxyOnLocal property to write a message to the console saying whether the proxy is used for local addresses or not.
WebProxy proxy = (WebProxy)GlobalProxySelection.Select;
Console.WriteLine("Address of the proxy server: {0}", proxy.Address);
foreach (string bypassAddress in proxy.BypassList)
{
Console.WriteLine("Not using the proxy server for this " +
"address: {0}", bypassAddress);
}
Console.WriteLine ("For local addresses the proxy server is {0} used",
proxy.BypassProxyOnLocal ? "not" : "");
In my case the output to the console is as follows:
Changing the WebProxy for Specific Requests
Other than using the default web proxy for all requests we can use a different proxy for specific requests. In a network, multiple proxy servers can be used either to distribute the load, or because of security requirements. To select a different proxy we just have to set the Proxy property of the WebRequest class.
WebRequest request = WebRequest.Create("http://www.wrox.com");
request.Proxy = new WebProxy("172.31.24.28", 8080);
The Proxy property of the WebRequest class accepts an object that implements the IWebProxy interface. Of course, the WebProxy class implements the IWebProxy interface. The constructor of the WebProxy class is overloaded and accepts a URI to the proxy server, and also all the parameters to configure a WebProxy object that we know already, for example, the user credentials, and the list of URIs where the proxy server should be bypassed, among others.