DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 13.6 Communicating with a Web Server

Problem

You want to send a request to a web server in the form of a GET or POST request. After you send the request to a web server, you want to get the results of that request (the response) from the web server.

Solution

Use the HttpWebRequest class in conjunction with the WebRequest class to create and send a request to a server.

Take the URI of the resource, the method to use in the request (GET or POST), and the data to send (only for POST requests), and use this information to create an HttpWebRequest:

using System.Net;
using System.IO;
using System.Text;

// ...

public static HttpWebRequest GenerateGetOrPostRequest(string uriString,
    string method, 
    string postData)
{
    if((method.ToUpper( ) != "GET") &&
        (method.ToUpper( ) != "POST"))
            throw new ArgumentException(method + 
               " is not a valid method.  Use GET or POST.","method");

    HttpWebRequest httpRequest = null;
    // get a URI object
    Uri uri = new Uri(uriString);
    // create the initial request
    httpRequest = (HttpWebRequest)WebRequest.Create(uri);

    // check if asked to do a POST request, if so then modify 
    // the original request as it defaults to a GET method
    if(method.ToUpper( )=="POST") 
    {
        // Get the bytes for the request, should be pre-escaped
        byte[] bytes = Encoding.UTF8.GetBytes(postData); 

        // Set the content type of the data being posted.
        httpRequest.ContentType=
            "application/x-www-form-urlencoded";

        // Set the content length of the string being posted.
        httpRequest.ContentLength=postData.Length;

        // Get the request stream and write the post data in
        Stream requestStream = httpRequest.GetRequestStream( );
        requestStream.Write(bytes,0,bytes.Length);
        // Done updating for POST so close the stream
        requestStream.Close( );
    }

    // return the request
    return httpRequest;
}

Once we have an HttpWebRequest, we send the request and get the response using the GetResponse method that takes our newly created HttpWebRequest as input and returns an HttpWebResponse. In this example, we perform a GET for the index.aspx page from the http://localhost/mysite web site:

HttpWebRequest request = 
 GenerateGetOrPostRequest("http://localhost/mysite/index.aspx",
                               "GET", 
                               null);

HttpWebResponse response = (HttpWebResponse) request.GetResponse( );
// This next line uses VerifyResponse from Recipe 13.5
if(VerifyResponse(response)==ResponseCategories.Success)
{
    Console.WriteLine("Request succeeded");    
}

We generate the HttpWebRequest, send it and get the HttpWebResponse, and then check the success using the VerifyResponse method from Recipe 13.5.

Discussion

The WebRequest and WebResponse classes encapsulate all of the functionality to perform basic web transactions. HttpWebRequest and HttpWebResponse are derived classes from these, respectively, and provide the HTTP specific web transaction support.

At the most fundamental level, to perform an HTTP-based web transaction, you use the Create method on the WebRequest class to get a WebRequest that can be cast to an HttpWebRequest (so long as the the scheme is http:// or https://). This HttpWebRequest is then submitted to the web server in question when the GetResponse method is called, and it returns an HttpWebResponse that can then be inspected for the response data.

See Also

See the "WebRequest Class," "WebResponse Class," "HttpWebRequest Class," and "HttpWebResponse Class" topics in the MSDN documentation.

    [ Team LiB ] Previous Section Next Section