如果HttpClient的是不应该在使用使用声明,请参阅以下链接: https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/ http://www.nimaara.com/2016/11 /01/beware-of-the-net-httpclient/
因此,如果您不应该在 using 语句中使用 HttpClient ,那么为什么有这么多示例将 WebClient 放在 using 语句中(包括 Microsoft);我还没有看到一篇关于不在using语句中放置 WebClient 的文章?最终在最低级别 WebClient 和 HttpClient 最终在同一个地方打开一个 TCP/IP 套接字。是否介于两者之间使 WebClient 可以放入using语句?
注意我提出这个问题是有原因的:我有一个在 WinXP、Win7 完整系统和嵌入式系统上运行的客户端应用程序,我用来将 XML POST 到我的后端主机(CentOS 系统)的代码如下所示:
void PostXml(string url, string xml, int id) {
try
{
//Console.WriteLine("POST Request: " + xml);
using (WebClient wc = new WebClient())
{
wc.Proxy = null; //DEBUG: testing this to see if it helps webclient post failure after time x
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
Byte[] d …Run Code Online (Sandbox Code Playgroud)