In a nutshell, WebRequest—in its HTTP-specific implementation, HttpWebRequest—represents the original way to consume HTTP requests in . NET Framework. WebClient provides a simple but limited wrapper around HttpWebRequest. And HttpClient is the new and improved way of doing HTTP requests and posts, having arrived with .
NET 4.5 platform the community developed an alternative. Today, RestSharp is one of the only options for a portable, multi-platform, unencumbered, fully open-source HTTP client that you can use in all of your applications. It combines the control of HttpWebRequest with the simplicity of WebClient .
The WebClient class provides common methods for sending data to or receiving data from any local, intranet, or Internet resource identified by a URI. The WebClient class uses the WebRequest class to provide access to resources.
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. Do not use the HttpWebRequest constructor. Use the WebRequest.
WebClient is a higher-level abstraction built on top of HttpWebRequest to simplify the most common tasks. For instance, if you want to get the content out of an HttpWebResponse, you have to read from the response stream:
var http = (HttpWebRequest)WebRequest.Create("http://example.com");
var response = http.GetResponse();
var stream = response.GetResponseStream();
var sr = new StreamReader(stream);
var content = sr.ReadToEnd();
With WebClient, you just do DownloadString
:
var client = new WebClient();
var content = client.DownloadString("http://example.com");
Note: I left out the using
statements from both examples for brevity. You should definitely take care to dispose your web request objects properly.
In general, WebClient is good for quick and dirty simple requests and HttpWebRequest is good for when you need more control over the entire request.
Also WebClient doesn't have timeout property. And that's the problem, because dafault value is 100 seconds and that's too much to indicate if there's no Internet connection.
Workaround for that problem is here https://stackoverflow.com/a/3052637/1303422
I know its too longtime to reply but just as an information purpose for future readers:
WebRequest
System.Object
System.MarshalByRefObject
System.Net.WebRequest
The WebRequest
is an abstract base class. So you actually don't use it directly. You use it through it derived classes - HttpWebRequest
and FileWebRequest
.
You use Create method of WebRequest
to create an instance of WebRequest
. GetResponseStream
returns data stream
.
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:
Example:
var _request = (HttpWebRequest)WebRequest.Create("http://stackoverflow.com");
var _response = (HttpWebResponse)_request.GetResponse();
WebClient
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Net.WebClient
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:
Example:
var _client = new WebClient();
var _stackContent = _client.DownloadString("http://stackoverflow.com");
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 an encoding.
NOTE: Basically "WebClient takes few lines of code as compared to WebRequest"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With