Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use WebClient with .NetCore?

Tags:

c#

.net

.net-core

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?

like image 978
TimBoss Avatar asked May 14 '15 00:05

TimBoss


People also ask

How do I use WebClient in .NET core?

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.

Is WebClient obsolete?

WebRequest, WebClient, and ServicePoint are obsolete.

Why do we use WebClient in C#?

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.

What can I use instead of WebClient?

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.


1 Answers

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.

like image 198
Matt DeKrey Avatar answered Oct 14 '22 01:10

Matt DeKrey