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 this, 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 being 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 to perform the request that you want. For example, you might need to set the credentials so that your request authenticates to the server, or you might need to set the proxy server so that your request goes through the correct proxy server. Both these items are covered later 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 also can find out when the resource was last updated. This might be important if you want to retrieve data, but it is time-sensitive and you should check the data. 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 . These headers can 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 26.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 name strURL ). A WebResponse object is obtained by calling the WebRequest GetResponse() method. A stream object is created, the encoding is specified, and a stream reader is also created. Then there is some fairly simple code to 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 26.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  } 

I have created a page in MyNetworkingDemo application named WebRequestDemo. It can be seen running in Figure 26.3.

Figure 26.3. WebRequest and WebResponse offer flexibility in retrieving data.

graphics/26fig03.gif

There are several choices with regards to the transport type when you retrieve information. The default is All, and this allows retrieval using all transport types. But you can be specific in the types you allow with the enumeration seen in Table 26.1.

Table 26.1. The Transport Type 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.

   


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