Using the WebRequest and WebResponse Classes

Using the WebRequest and WebResponse Classes

A combination of WebRequest and WebResponse classes can be used to request data from Internet resources, and then to read the response. To do these actions, the first thing you must do is create a WebRequest object, as the following code shows you:

 WebRequest wReq = WebRequest.Create( "http://www.SomeDomain.com/" ); 

The .NET Framework provides protocol-specific WebRequest and WebResponse objects for resources that begin with HTTP, HTTPS, and FILE. To access other protocols, you must implement protocol-specific descendants of WebRequest and WebResponse. This chapter deals only with HTTP and HTTPS data.

The next thing you should do is to set any properties required to perform the data retrieval request that you want. For instance, you may need to set the credentials so that your request authenticates to the server, or you may need to set the proxy server so that your request goes through the correct proxy server. Both these items are covered later on in this chapter.

In most cases, the WebRequest instance itself is sufficient to send and receive data. The following code shows how to use a WebRequest object to get a WebResponse object:

 WebResponse wResp = wReq.GetResponse(); 

You can also find out when the resource was last updated. This information might be important if you want to retrieve data so that the time-sensitive data can be accurately evaluated. The following code shows you how to check the date of a resource:

 if( wResp is HttpWebResponse ) {     DateTime Updated = (HttpWebResponse)wResp)).LastModified; } 

You can also get access to the HTTP headers that are contained within the WebResponse object. These headers might include the server, the content type, the content length, and so forth. The following example shows you how to retrieve the server header from the headers collection:

 string server = wResp.Headers["Server"]; 

In Listing 13.3, code that uses a WebRequest and a WebResponse object shows you how to retrieve data over the Internet. The WebRequest object is created based on a URL (and the URL specification is in a string named strURL). A WebResponse object is obtained by calling the WebRequest GetResponse() method. A Stream object is created, the encoding is specified, and a StreamReader object is also created. Then some fairly simple code lets you retrieve the entire contents of the resource. In the case of this example, nothing is done with the data; it is simply assigned to a string. But in most cases, you would be storing the data to a disk file or some sort of a user interface object.

Listing 13.3 Using WebRequest and WebResponse Objects to Retrieve Data
 try {     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 } 

Table 13.2. The TransportType Enumeration

Member name

Description

All

All transport types.

Connectionless

The transport type is connectionless, such as UDP.

ConnectionOriented

The transport is connection-oriented, such as TCP.

Tcp

TCP transport.

Udp

UDP transport.

I have created a page in MyNetworkingDemo application named WebRequestDemo.

You have a number of choices with regard to the transport type when you retrieve information. The default is All, which allows retrieval using all transport types. But you can be specific in the types you allow with the enumeration seen in Table 13.2.



ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ISBN: 321159659
EAN: N/A
Year: 2003
Pages: 175

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