DCSIMG
Using the HttpWebRequest Class - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2013 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

Using the HttpWebRequest Class

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
Posted: Aug 23 2009, 12:08 PM by Gil Fink | with 3 comment(s) |
תגים:,

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# August 23, 2009 1:10 PM

Uri Lavi said:

It's preferable to use WebClient

# August 24, 2009 10:27 PM

Gil Fink said:

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.

# August 25, 2009 11:06 AM