如何在 .NetCore 中使用 WebClient? [英] How to use WebClient with .NetCore?
问题描述
有没有办法在 .NET Core 应用程序中使用 WebClient
?如果我构建应用程序,我会收到以下错误:
Is there any way to use a WebClient
in a .NET Core application?
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?)
我认为 WebClient
不是 .NET Core 的一部分,但还有其他选择吗?
I think WebClient
is not a part of .NET Core, but is there any alternative?
推荐答案
从 .Net Standard 2.0 开始,WebClient 现在可用于该标准的任何实现,包括 .Net Core.但是,堆栈溢出问题需要帮助在 HttpClient 和 WebClient 之间做出决定"有一些相当不错的答案,说明为什么您应该使用 HttpClient
代替.
As of .Net Standard 2.0, WebClient is now available to any implementations of the standard, including .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
中没有内置的进度报告.但是,因为它使用流,所以可以编写自己的.如何为便携式HttpClient实现进度报告"的答案提供了一个报告响应流进度的示例.
One of the drawbacks mentioned is that there is no built-in progress reporting in the 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.
如果您的目标是标准的早期版本,则需要使用 HttpClient
,因为 WebClient
在 .Net Standard 2.0 之前不可用.
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.
这篇关于如何在 .NetCore 中使用 WebClient?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!