Reading GET response as string using System.Net.HttpClient

suggest change

On .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;
    }
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents