Using the WebProxy Class

   

Using the WebProxy Class

There might 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 that 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( "http://www.SomeDomain.com" );  req.Proxy = proxyObject; 

When the proxy object is created, the name of the proxy server is specified (in this case, MyProxyServer), the port number (80), and whether to bypass the proxy for local servers (true in this case). 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 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 26.4. This example creates a WebProxy object and pulls data from a remote URL. Several 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 26.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 26.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 while access the local host. Then the global proxy settings for this machine are set to be the same as the proxy 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, and this provides flexibility because Stream objects offer a lot of choices when deciding how to process data. In Listing 26.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. But in most cases, applications would be saving it to disk or assigning it to some sort of user interface object.

   


Special Edition Using ASP. NET
Special Edition Using ASP.Net
ISBN: 0789725606
EAN: 2147483647
Year: 2002
Pages: 233

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net