.net WebClient 与 HttpWebRequest/HttpWebResponse
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1694388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WebClient vs. HttpWebRequest/HttpWebResponse
提问by Dan
It seems to me that most of what can be accomplished with HttpWebRequest/Responsecan also be accomplished with the WebClientclass. I read somewhere that WebClientis a high-level wrapper for WebRequest/Response.
So far, I can't see anything that can be accomplished with HttpWebRequest/Responsethat can not be accomplished with WebClient, nor where HttpWebRequest/Response will give you more "fine-grained" control.
在我看来,大部分可以完成的事情HttpWebRequest/Response也可以通过WebClient类来完成。我在某处读到WebClient了WebRequest/Response.
到目前为止,我看不到任何可以与完成HttpWebRequest/Response不能用完成WebClient,也没有在那里的HttpWebRequest /响应会给你更多的“细粒度”控制。
When should I use WebClient and when HttpWebRequest/Response? (Obviously, HttpWebRequest/Responseare HTTP specific.)
我应该什么时候使用 WebClient,什么时候使用HttpWebRequest/Response?(显然,HttpWebRequest/Response是特定于 HTTP 的。)
If HttpWebRequest/Responseare lower level then WebClient, what can I accomplish with HttpWebRequest/Responsethat I cannot accomplish with WebClient?
如果HttpWebRequest/Response是低级的话WebClient,我能用什么来完成HttpWebRequest/Response我不能用的WebClient?
采纳答案by Thomas Levesque
Using HttpWebRequestgives you more control on the request. You can set cookies, headers, protocol, etc... In the response, you can also retrieve the cookies and headers
使用可以HttpWebRequest让您更好地控制请求。您可以设置cookies、headers、协议等...在响应中,您还可以检索cookies和headers
回答by feroze
HttpWebRequestexposes a lot more stuff that allows you fine grained protocol control, for eg: whether you want to use Keep-Alive, what connection pool to use, whether to buffer writes or not, etc.
HttpWebRequest公开了更多允许您进行细粒度协议控制的内容,例如:是否要使用 Keep-Alive、要使用的连接池、是否缓冲写入等。
WebClientdoes not expose all of those (although you can subclass from WebClientand getaccess to the underlying Request object).
WebClient不会公开所有这些(尽管您可以从WebClient底层请求对象子类化并获取访问权限)。
WebClientis useful for those situations where you just want to do an operation (eg: POST/GET/Form upload) and cant be bothered to create and manage the HttpWebRequest, RequestStream, HttpWebResponse, and response stream.
WebClient是,你只想做一个操作的情况下非常有用(如:POST / GET /表格上传),并不能费心去创建和管理HttpWebRequest,RequestStream,HttpWebResponse,和响应流。
回答by Benjamin Cox
From Tim Heuer's blog - http://timheuer.com/blog/archive/2008/03/14/calling-web-services-with-silverlight-2.aspx
来自 Tim Heuer 的博客 - http://timheuer.com/blog/archive/2008/03/14/calling-web-services-with-silverlight-2.aspx
Instead in Silverlight you'll want to use WebClient or HttpWebRequest. What's the difference? Here's the timheuer version. WebClient is a simpler implementation doing GET requests really easily and get a response stream. HttpWebRequest is great for when you need a bit more granular control over the request, need to send headers or other customizations.
在 Silverlight 中,您需要使用 WebClient 或 HttpWebRequest。有什么不同?这是 timheuer 版本。WebClient 是一个更简单的实现,可以非常轻松地执行 GET 请求并获取响应流。HttpWebRequest 非常适合当您需要对请求进行更精细的控制、需要发送标头或其他自定义项时。
回答by Baaziz
The WebClientclass runs on the user interface thread, so the user interface is not responsive while data is being downloaded from the Internet. On the other hand, the HttpWebRequestclass does not block the user interface thread, and your application is responsive. So, in apps where a large amount of data is to be downloaded from the Internet or if the source of the data is slow to access, you should use the HttpWebRequest class; in all other cases, you should use the WebClient class.
在Web客户端上的用户界面线程类运行,所以当数据从互联网上下载的用户界面没有响应。另一方面,HttpWebRequest类不会阻塞用户界面线程,并且您的应用程序是响应式的。因此,在需要从互联网下载大量数据或数据源访问速度较慢的应用程序中,您应该使用HttpWebRequest类;在所有其他情况下,您应该使用 WebClient 类。
回答by Sam
Another disadvantage of WebClientis it ignores the HTTP ContentType's charsetvalue when you use it to get the response text. You have to explicitly set the encoding via the Encodingproperty.
另一个缺点WebClient是当您使用它来获取响应文本时,它会忽略 HTTPContentType的charset值。您必须通过Encoding属性显式设置编码。
回答by Julio Spader
The "HtttpWebRequest" is obsolete in .NET 4.5. Now, this class is internal only.
“HtttpWebRequest”在 .NET 4.5 中已过时。现在,这个类只是内部的。
回答by Zain Ali
One more thing HttpWebrquest allows you compression but he Net.WebClient class doesn't support HTTP compression
另一件事 HttpWebrquest 允许您进行压缩,但 Net.WebClient 类不支持 HTTP 压缩
回答by synergetic
One example: Posting data and getting back processed data in one request/response cycle seems to be impossible with WebClient, but you can do that with HtttpWebRequest.
一个例子:使用 WebClient 在一个请求/响应周期中发布数据和取回处理过的数据似乎是不可能的,但您可以使用 HttpsWebRequest 做到这一点。

