I was wondering if somebody explain the difference for the listed classes
-
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 Answers
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.

- 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
-
-
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
-
1I 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
-
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
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 theWebRequest
class while protocol-specific descendant classes carry out the details of the request.(...)
Because the
WebRequest
class is an abstract class, the actual behavior ofWebRequest
instances at run time is determined by the descendant class returned byCreate
method. For more information about default values and exceptions, see the documentation for the descendant classes, such asHttpWebRequest
andFileWebRequest
.
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 inWebRequest
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 theWebRequest.Create
method to initialize newHttpWebRequest
objects. If the scheme for the Uniform Resource Identifier (URI) is http:// or https://,Create
returns anHttpWebRequest
object.The
GetResponse
method makes a synchronous request to the resource specified in theRequestUri
property and returns anHttpWebResponse
that contains the response. You can make an asynchronous request to the resource using theBeginGetResponse
andEndGetResponse
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.