Recipe14.6.Communicating with a Web Server


Recipe 14.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, as shown in Example 14-3.

Example 14-3. Communicating with a web server

 using System.Net; using System.IO; using System.Text; // … // GET overload public static HttpWebRequest GenerateHttpWebRequest(string UriString) {     // Get a Uri object.     Uri Uri = new Uri(UriString);     // Create the initial request.     HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(Uri);     // Return the request.     return httpRequest; } // POST overload public static HttpWebRequest GenerateHttpWebRequest(string UriString,     string postData,     string contentType) {     // Get a Uri object.      Uri Uri = new Uri(UriString);     // Create the initial request.     HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(Uri);     // 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 = contentType;         //"application/x-www-form-urlencoded"; for forms     // Set the content length of the string being posted.     httpRequest.ContentLength = postData.Length;     // Get the request stream and write the post data in.     using (Stream requestStream = httpRequest.GetRequestStream())     {         requestStream.Write(bytes, 0, bytes.Length);     }     // Return the request.     return httpRequest; } 

Once you have an HttpWebRequest, you send the request and get the response using the Getresponse method. It takes the newly created HttpWebRequest as input and returns an HttpWebResponse. The following example performs a GET for the index.aspx page from the http://localhost/mysite web site:

 HttpWebRequest request =  GenerateHttpWebRequest(http://localhost/mysite/index.aspx); using(HttpWebResponse response = (HttpWebResponse) request.GetResponse( )) {     // This next line uses VerifyResponse from Recipe 14.5.     if(VerifyResponse(response)==ResponseCategories.Success)     {         Console.WriteLine("Request succeeded");     } } 

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

Discussion

The WebRequest and WebResponse classes encapsulate all of the functionality to perform basic web communications. HttpWebRequest and HttpWebResponse are derived from these classes and provide the HTTP-specific 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 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.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

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