Using the HttpWebRequest Class

August 23, 2009

tags: ,
2 comments

Using the HttpWebRequest Class

Last week I got a Using the HttpWebRequest Class
requirement for a
direct interaction
with an HTTP
server. As a result I
needed to use the
HttpWebRequest class in
order to create the relevant requests. 
This post will introduce the HttpWebRequest class.

The HttpWebRequest Class

The HttpWebRequest class is a wrapper class that wrap an HTTP
request for a resource. It provides many properties and methods that
enable us to configure an HTTP request and interact with HTTP servers.
You should initialize an HttpWebRequest only through the WebRequest’s
Create method like in the following example:

var request = (HttpWebRequest)WebRequest.Create("http://blogs.microsoft.co.il");

The most useful methods that you should know are the GetResponse,

which returns an HttpWebResponse, and BeginGetResponse and EndGetResponse

methods which enables the making of asynchronous request to the relevant

resource. The following example shows how get a response from a HttpWebRequest 
object:

var response = (HttpWebResponse)request.GetResponse();

Example of Using the HttpWebRequest Class

The following code is an example of how to use the HttpWebRequest in

order to get the web page of http://blogs.microsoft.co.il (this site)

as a string:

try
{
    var request = (HttpWebRequest)WebRequest.Create("http://blogs.microsoft.co.il");
    request.Method = "GET";
    request.ContentType = "text/html";                
    request.KeepAlive = false;
    request.UseDefaultCredentials = true;                
 
    var response = (HttpWebResponse)request.GetResponse();
 
    using (var stream = new StreamReader(response.GetResponseStream()))
    {
        var result = stream.ReadToEnd();
        return result;                    
    }
}
catch (WebException ex)
{
    // do something
}
catch (Exception ex)
{
    // do something
}

Summary

In this post we saw the HttpWebRequest class and how to use it

to get a resource like a web page to our application. The HttpWebRequest 
enables us to communicate with HTTP servers by building the relevant

requests we want to perform and sending them. There are many

other details about this class that I encourage you to check out. Just

Google it/Bing it and you’ll find more details.

DotNetKicks Image
Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

2 comments

  1. Uri LaviAugust 24, 2009 ב 22:27

    It’s preferable to use WebClient

    Reply
  2. Gil FinkAugust 25, 2009 ב 11:06

    Hi Uri,
    It depends on what you want to achieve.
    In my situation the WebClient isn’t preferable since I need more control on the requests I send. In the simple example that I provided, I agree that using WebClient is preferable.

    Reply