问题
What difference is there between the WebClient
and the HttpWebRequest
classes in .NET? They both do very similar things. In fact, why weren't they merged into one class (too many methods/variables etc may be one reason but there are other classes in .NET which breaks that rule).
Thanks.
回答1:
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.
回答2:
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
回答3:
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://stackverflow.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://stackverflow.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 and encoding.
NOTE: Basically "WebClient takes few lines of code as compared to Webrequest"
来源:https://stackoverflow.com/questions/4988286/what-difference-is-there-between-webclient-and-httpwebrequest-classes-in-net