15.8 Using the WebRequest Class for HTTP Communication

 <  Day Day Up  >  

15.8 Using the WebRequest Class for HTTP Communication

You want to download an Internet resource using a WebRequest object using HTTP .


Technique

You can use the WebRequest class to perform communication using HTTP without having to implement the protocol at a lower level using sockets. To download an Internet resource using the WebRequest class, create a new WebRequest object by using the static method Create . Once you make the request, you can start streaming in the data. However, to avoid latency issues, you might want to specify a timeout period for unresponsive servers. To do so, set the Timeout method to a value in milliseconds :

 
 // create a web request using web address in text box WebRequest request = WebRequest.Create( tbWebAddress.Text ); // set timeout to 10 seconds request.Timeout = 10000; 

After you make an initial request, you must then create a WebResponse object to handle the data that is sent by the server. Calling the GetResponse method from the request object created earlier:

 
 WebResponse response = request.GetResponse(); 

The WebResponse class contains a stream object that you can use to stream the data from the server to your application. You use the stream object just like the stream classes discussed in Chapter 12. To access the WebResponse stream, call the GetResponseStream method, passing the returned Stream object to a StreamReader that can be used to read the data into a buffer. Finally, close the stream object after the data stream is read:

 
 // get response stream and create a stream reader Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream, Encoding.ASCII); // read in data and set as textbox text String content = reader.ReadToEnd(); tbContent.Text = content; responseStream.Close(); 

Comments

The WebRequest class is abstract, which means you are unable to directly instantiate it using the new keyword. The preceding example creates a WebRequest object by calling the static Create method in the WebRequest class to create a new request. The parameter to this method is a Uniform Resource Identifier (URI). Depending on the protocol specified in the URI, the WebRequest class creates an appropriate object for that protocol. This example creates an HttpWebRequest object, which means you can cast the WebRequest object to HttpWebRequest to gain access to more specific HTTP properties.

HTTP headers accompany both a request and response object. Headers are a part of the HTTP protocol that facilitate communication between a server and a connected client application. The headers themselves are nothing more than key/value pairs, but you can change their values to change the way the server interacts with your application. You can retrieve HTTP headers by directly accessing the Headers collection defined in the WebResponse class:

 
 StringBuilder headerValues = new StringBuilder(); foreach( string key in response.Headers.AllKeys ) {     headerValues.Append( "Key: " + key + " Value: " + headers.Get(key) + "\n" ); } MessageBox.Show( headerValues.ToString(), "Header Collection" ); 

This headers collection, because it's a dictionary-based collection, contains an indexer that accepts a string object representing the header key. To alleviate any case issues or to avoid incorrectly specifying a header key, you can also cast the WebResponse object to an HttpWebResponse object and use the several different properties the HttpWebResponse class exposes to view header values:

 
 MessageBox.Show(((HttpWebRequest)wReq).UserAgent); 
 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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