[英]How to use WebClient with .NetCore?
Is there any way to use a WebClient
in a .NET Core application? 有什么方法可以在.NET Core应用程序中使用WebClient
? If I build the application I get the following error: 如果我构建应用程序,则会出现以下错误:
Severity Code Description Project File Line
Error CS0246 The type or namespace name 'WebClient' could not be found (are you missing a using directive or an assembly reference?)
I think WebClient
is not a part of .NET Core, but is there any alternative? 我认为WebClient
不是.NET Core的一部分,但是还有其他选择吗?
As of .Net Standard 2.0, WebClient is now available to any implementations of the standard, including .Net Core. 从.Net Standard 2.0开始,WebClient现在可用于该标准的任何实现,包括.Net Core。 However, the Stack Overflow question " Need help deciding between HttpClient and WebClient " has some fairly good answers as to why you should be using the HttpClient
instead. 但是,堆栈溢出问题“ 需要帮助来决定HttpClient和WebClient之间的关系 ”对于为什么应使用HttpClient
却有一些相当不错的答案。
One of the drawbacks mentioned is that there is no built-in progress reporting in the HttpClient
. 提到的缺点之一是HttpClient
没有内置的进度报告。 However, because it is using streams, it is possible to write your own. 但是,由于它使用流,因此可以编写自己的流。 The answers to " How to implement progress reporting for Portable HttpClient " provides an example for reporting the progress of the response stream. “ 如何为Portable HttpClient实现进度报告 ”的答案提供了一个报告响应流进度的示例。
If you're targeting prior versions of the standard, you'll need to use HttpClient
as WebClient
is not available prior to .Net Standard 2.0. 如果您要针对该标准的HttpClient
版本,则需要使用HttpClient
因为.Net Standard 2.0之前WebClient
不可用。
WebClient
现在在.Net Standard 2.0中可用https://docs.microsoft.com/en-gb/dotnet/api/system.net.webclient?view=netstandard-2.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.