Using the WebProxy Class

Using the WebProxy Class

There may be times when your application needs to communicate through a proxy server. When this is true, it would be advisable to use the WebProxy class so you can have control over the communication. You can specify the name of the proxy server, the port through which you will be communicating, and whether to bypass the proxy server for local communications. The following code snippet shows how you can use the WebProxy class to make a Web request:

 WebProxy proxyObject = new WebProxy( "http://MyProxyServer:80/", true ); WebRequest req = WebRequest.Create( "" ); req.Proxy = proxyObject; 

When the Proxy object is created, the name of the proxy server (in this case, MyProxyServer), the port number (80), and whether to bypass the proxy for local servers (true in this case) are specified. The second line of this example creates a WebRequest object. The third line sets the WebRequest object's proxy field to the newly created Proxy object. This setting causes the WebRequest to go through the proxy server that was specified in the creation of the WebProxy object.

A more complete example can be seen in Listing 13.4. This example creates a WebProxy object and pulls data from a remote URL. A couple of string variables used in this example need some explanation. The first is strProxyName. This is a string that should contain the name of a proxy server. The other string variable that I am using in this example is strURL. This string contains the URL identifier for the data that you want to retrieve.

Listing 13.4 Using a WebProxy Object to Exercise Control over the Data Retrieval Operation
 try {     WebProxy ProxyObj = new WebProxy( strProxyName, 80 );     // Disable Proxy use when the host is local i.e. without periods.     ProxyObj.BypassProxyOnLocal = true;     // Now actually take over the global with new settings,     // all new requests     // use this proxy info     GlobalProxySelection.Select = ProxyObj;     WebRequest req = WebRequest.Create( strURL );     WebResponse result = req.GetResponse();     Stream ReceiveStream = result.GetResponseStream();     Encoding encode = System.Text.Encoding.GetEncoding( "utf-8" );     StreamReader sr = new StreamReader( ReceiveStream, encode );     Char[] ReadBuffer = new Char[256];     int nCount = sr.Read( ReadBuffer, 0, 256 );     while( nCount > 0 )     {         String str = new String( ReadBuffer, 0, nCount );         nCount = sr.Read( ReadBuffer, 0, 256 );     } } catch( Exception ) {     // Handle the exception here } 

If you look at the code in Listing 13.4, you will see that the first thing that happens is that the WebProxy object is created. The next thing that is done is to specify that the proxy server is bypassed access the local host. Then the global proxy settings for this machine are set to be the same as the WebProxy object that was just created. A WebRequest object is then created based on the URL in the strURL string. A WebResponse object is created that will be used to obtain any data that is sent back. A Stream object is then created, which provides flexibility because Stream objects offer a lot of choices when deciding how to process data. In Listing 13.4, you will see a simple do/while loop that reads in data and assigns the character data to a string. In this case, the code is not doing anything with the data except simply reading it in, although most applications do some useful work with the information.

ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ISBN: 321159659
Year: 2003
Pages: 175 © 2008-2017.
If you may any questions please contact us: