What are all the differences? Well, first off, let’s look at the inheritance hierarchy for each one of them.
Based on that findings, we know that HttpWebRequest inherits from WebRequest. They both have general purposes of sending and receiving data through a network connection. There are also FileWebRequest and FtpWebRequest classes that inherit from WebRequest. Normally, you would use WebRequest to, well, make a request and convert the return to either HttpWebRequest, FileWebRequest or FtpWebRequest, depend on your request. Below is an example:
var _request = (HttpWebRequest)WebRequest.Create("https://stack247.wordpress.com");
var _response = (HttpWebResponse)_request.GetResponse();
WebClient provides common operations to sending and receiving data from a resource identified by a URI. Simply, it’s a higher-level abstraction of HttpWebRequest. This ‘common operations’ is what differentiate WebClient from HttpWebRequest, as also shown in the sample below:
var _client = new WebClient();
var _stack247Content = _client.DownloadString("https://stack247.wordpress.com");</code>
There are also DownloadData and DownloadFile operations under WebClient instance. These common operations also simplify code of what we would normally do with HttpWebRequest. Using HttpWebRequest, we have to get the response of our request, instantiate StreamReader to read the response and finally, convert the result to whatever type we expect. With WebClient, we just simply call DownloadData, DownloadFile or DownloadString.
However, keep in mind that WebClient.DownloadString doesn’t consider the encoding of the resource you requesting. So, you would probably end up receiving weird characters if you don’t specify and encoding.
Unlike any other classes mentioned in this post, HttpRequest has nothing to do with sending and receiving data. Well, sort of. When a request is made from a client to your application, you can access this request through HttpRequest class.
In short, it’s kind of like this: HttpWebRequest, WebRequest and WebClient are client-classes which mean they are the one making a request to a resource, or a server. HttpRequest is sitting on that resource’s (or server’s) side, interpreting the request by reading its HttpRequest object.