Communicating with Remote Servers through the HTTP Protocol


Up until now we have focused on how to work with socket programming that passes arbitrary data between a client and a host by using either TCP or UDP packets. In either case we have invented our own protocol for communication. For example, the managed chat application used a protocol where ChatPacket objects were serialized into bytes and sent through the network connection. Our simple Remote Hello and UDPHello examples simply sent strings back and forth. In each of these cases, the client and the host both knew what to expect to come down the wire at any given time. As developers, we are free to design our protocols and set those expectations as we see fit.

While this approach provides a lot of flexibility, there are many servers on the Internet that follow a much more specific communication protocol, HTTP, which is the protocol used on the World Wide Web. When using the HTTP protocol, there are very specific rules about how a client contacts a server and what the client can ask for at any specific time. The data that an HTTP server returns will arrive as a set of TCP packets, but wading through all of the protocol- related information is a very tedious task. A transaction with an HTTP server is structured as follows :

  1. The client connects to the HTTP server.

  2. The HTTP server responds.

  3. The client requests data by using GET or asks to place data by using a POST command.

  4. The server responds to the request and hands down an error code if the client request cannot be fulfilled. For example, the famous error code 404 is returned if the client tries to GET a file that does not exist.

  5. Step 4 is repeated an arbitrary number of times.

  6. The client closes the connection.

Each time the client makes a request or the server responds, a brand-new socket connection is made with the server, and the data that comes down the wire must be parsed and processed by the client. Setting up all of the socket code by hand would be quite tedious. The HttpWebRequest class streamlines all of the processes involved with interacting with an HTTP server. Specifically, HttpWebRequest can perform these tasks for you:

  • Initialize a connection with an HTTP server

  • Receive the response from an HTTP server

  • Return a stream that holds the data that the HTTP server sent back as a result of your request

To summarize, the value of the HttpWebRequest class is that it frees the developer from having to do any TCP-style socket programming when interacting with a Web server. Developers can instantiate an instance of an HttpWebRequest and get the response from the server with only a few lines of code.

Nuts-and-Bolts Usage of HttpWebRequest

To use an HttpWebRequest class to download information from an HTTP server, follow these steps:

  1. Create an instance of the Uri class to hold the location of the HTTP server.

  2. Instantiate an HttpWebRequest by using the Uri of step 1.

  3. Ask the HttpWebRequest to return the response from the Web server in the form of a Stream class.

  4. Consume the contents of the Stream .

Coding Sample for HttpWebRequest

The HttpWebRequest class reduces the complex task of contacting an HTTP server into just four steps, as performed by this code snippet:

 
 C# Uri l_Uri = new Uri("http://www.myserver.com"); HttpWebRequest l_WebReq = (HttpWebRequest)WebRequest.Create(l_Uri); HttpWebResponse l_WebResponse =         (HttpWebResponse)l_WebReq.GetResponse(); Stream l_responseStream = l_WebResponse.GetResponseStream(); StreamReader l_SReader = new StreamReader(l_responseStream); // Do something with l_SReader. For example, if you downloaded a // Web page, you could // extract the HTML code that came in the response and paint it on // the screen. VB Dim l_Uri As Uri l_Uri = New Uri(Me.txtRemoteIP.Text) Dim l_WebReq As System.Net.HttpWebRequest l_WebReq = System.Net.WebRequest.Create(l_Uri) Dim l_WebResponse As System.Net.WebResponse l_WebResponse = l_WebReq.GetResponse() Dim l_SReader As System.IO.StreamReader l_SReader = New         System.IO.StreamReader(l_WebResponse.GetResponseStream()) ' Do something with l_SReader. For example, if you downloaded a Web ' page, you could ' extract the HTML code that came in the response and paint it on ' the screen. 

Writing Applications with the HttpWebRequest : WebHello Sample

Our sample application uses an HttpWebRequest to contact the server and an HttpWebResponse to create a stream holding the response. The application acquires a stream from the WebResponse and paints the text into a TextBox. The source code for Web Hello is in SampleApplications\Chapter5\WebHello_CSharp and SampleApplications\Chapter5\WebHello_VB .



Microsoft.NET Compact Framework Kick Start
Microsoft .NET Compact Framework Kick Start
ISBN: 0672325705
EAN: 2147483647
Year: 2003
Pages: 206

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