32

I was wondering if somebody explain the difference for the listed classes

RB.
  • 36,301
  • 12
  • 91
  • 131
user970742
  • 419
  • 1
  • 5
  • 10
  • Two related questions: http://stackoverflow.com/questions/4457791/what-the-difference-between-webrequest-and-httpwebrequest and http://stackoverflow.com/questions/896253/c-sharp-httpwebrequest-vs-webrequest both of which with different answers which, to me, doesn't answer the main question here. – cregox Jun 10 '13 at 14:40

2 Answers2

45

They do different things.

WebRequest is the abstract base class for HttpWebRequest - you can't use it directly. It is the base class of other *Request classes (for FTP, File and other types of web requests). These classes are all used for getting resources (files) from the web.

There is also a WebClient class - probably the simplest to use of all the BCL classes designed to retrieve a resource from the Internet.

HttpRequest, on the other hand represents a request for a resource in an ASP.NET application - this is the server side of a Request.

The main difference is that HttpWebRequest is an HTTP client, and HttpRequest is server side to be used in an ASP.NET web application.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • about WebRequest I got. But what the difference between HttpRequest and HttpWebRequest? – user970742 Nov 21 '11 at 09:44
  • 1
    How come *"you can't use it directly"*? I use it all the time! – cregox Jun 10 '13 at 14:22
  • @Cawas - You can instantiate `WebRequest`? – Oded Jun 10 '13 at 14:24
  • From my WebAsync class, which I'm trying to improve: `WebRequest webRequest = WebRequest.Create(httpSite);` – cregox Jun 10 '13 at 14:24
  • 2
    @Cawas - The returned type will not be a `WebRequest` instance, but one of the inheriting classes (say `HttpWebRequest`, `FtpWebRequest` etc...). – Oded Jun 10 '13 at 14:26
  • 1
    I see... Then, why would I want to enforce and instantiate any other inherited class if this is way more dynamic? Just for code design? If so, then to me, this is the answer for the difference (between `HttpWebRequest` and `WebRequest`, anyway). – cregox Jun 10 '13 at 14:32
  • 1
    WebClient uses WebRequest under the hood. Nice! – Gerard ONeill Oct 21 '13 at 21:45
  • You can not instantiate an abstract class, but you can directly reference static members of that class without having to use a derived class, so you can call static method WebRequest.Create() as you do not need an instance of a class to call a static method. – Antony Booth Jul 15 '14 at 19:18
10

HttpRequest, as Oded already said, is a complete different thing - it generates HTTP Requests at server side so clients (such as HttpWebRequest) can read it.

Now, between HttpWebRequest and WebRequest, while one simply inherits from the other, in practice, there's no difference if you're using http:// when creating the request. Seems like it's all just about code design.

To use WebRequest we need to Create, which will return a descendant class. And as long as we use its instance, it will be acting just as an wrapper to the descendant class. Here's a remark from the docs:

WebRequest is the abstract base class for the .NET Framework's request/response model for accessing data from the Internet. An application that uses the request/response model can request data from the Internet in a protocol-agnostic manner, in which the application works with instances of the WebRequest class while protocol-specific descendant classes carry out the details of the request.

(...)

Because the WebRequest class is an abstract class, the actual behavior of WebRequest instances at run time is determined by the descendant class returned by Create method. For more information about default values and exceptions, see the documentation for the descendant classes, such as HttpWebRequest and FileWebRequest.

Disclaimer

I only got to know this now because, while also looking for the answer (which I didn't know 1h ago), I got this from forums at codeguru and MSDN doc remarks, all thanks to google:

The HttpWebRequest class provides support for the properties and methods defined in WebRequest and for additional properties and methods that enable the user to interact directly with servers using HTTP.

And the remarks go on (selected just ones with some relevancy to the difference):

Do not use the HttpWebRequest constructor. Use the WebRequest.Create method to initialize new HttpWebRequest objects. If the scheme for the Uniform Resource Identifier (URI) is http:// or https://, Create returns an HttpWebRequest object.

The GetResponse method makes a synchronous request to the resource specified in the RequestUri property and returns an HttpWebResponse that contains the response. You can make an asynchronous request to the resource using the BeginGetResponse and EndGetResponse methods.

HttpWebRequest exposes common HTTP header values sent to the Internet resource as properties, set by methods, or set by the system;

Then there's a table there I won't quote here.

Community
  • 1
  • 1
cregox
  • 17,674
  • 15
  • 85
  • 116