John John
John John

Reputation: 1

How to Replace WebClient with HttpClient?

I have the following WebClient inside my asp.net mvc web application:

using (WebClient wc = new WebClient()) // call the Third Party API to get the account id 
{
     string url = currentURL + "resources/" + ResourceID + "/accounts?AUTHTOKEN=" + pmtoken;
     var json = await wc.DownloadStringTaskAsync(url);
 }

So can anyone advise how I can change it from WebClient to be HttpClient?

Upvotes: 31

Views: 38076

Answers (1)

Hakan Fıstık
Hakan Fıstık

Reputation: 19521

// Injecting HttpClient would be a better idea if possible
HttpClient client = new();
string page = await client.GetStringAsync("page URL here");

Upvotes: 46

Related Questions