Reading GET response as string using System.Net.HttpClient
suggest changeOn .NET Framework 4.5 and later, HttpClient is in the System.Net.Http assembly. The Microsoft HTTP Client Libraries NuGet package backports it to .NET Framework 4.0. Calling .Result blocks, and on a UI or ASP.NET request thread it can deadlock. Prefer await inside an async method.
string requestUri = "http://www.example.com";
string responseData;
using (var client = new HttpClient())
{
using(var response = client.GetAsync(requestUri).Result)
{
response.EnsureSuccessStatusCode();
responseData = response.Content.ReadAsStringAsync().Result;
}
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents