Search code examples
c#.netwebclientwebrequest

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?


Solution

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