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?)
I think WebClient
is not a part of .NET Core, but is there any alternative?
You will need to import the following namespace. The Controller consists of the following Action method. Inside this Action method, first the JSON string is downloaded from an API using DownloadString method of the WebClient class. Note: SecurityProtocol needs to be set to TLS 1.2 (3072) in order to call an API.
WebRequest, WebClient, and ServicePoint are obsolete.
The WebClient class provides common methods for sending data to or receiving data from any local, intranet, or Internet resource identified by a URI. The WebClient class uses the WebRequest class to provide access to resources.
The HttpWebRequest class provides a lot of control over the request/response object. However, you should be aware that HttpClient was never designed to be a replacement for WebClient. You should use HttpWebRequest instead of HttpClient whenever you need the additional features that HttpWebRequest provides.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With