System.Net.Http namespace provides a class (HttpClient) for sending HTTP requests and receiving HTTP responses from a resource identified by a URI.

HttpClient is useful when downloading large amounts of data (50 megabytes or more), and the app should stream those downloads and not use the default buffering. If the default buffering is used the client memory usage will get very large, potentially resulting in substantially reduced performance.

Code

  1. string ReadResponseFromUrl(string url)
  2. {
  3. var httpClientHandler = new HttpClientHandler();
  4. var httpClient = new HttpClient(httpClientHandler)
  5. {
  6. BaseAddress = new Uri(url)
  7. };
  8. using (var response = httpClient.GetAsync(url))
  9. {
  10. string responseBody = response.Result.Content.ReadAsStringAsync().Result;
  11. return responseBody;
  12. }
  13. }

The HttpClient is a high-level API that wraps the lower-level functionality available on each platform where it runs. On each platform, HttpClient tries to use the best available transport:
HttpClient is intended to be instantiated once and re-used throughout the life of an application.