HttpClient Class
Provides a base class for sending HTTP requests and receiving HTTP responses from a resource identified by a URI.
Syntax
public class HttpClient : IDisposable
Remarks
The HttpClient class is a fundamental component for making HTTP requests in .NET. It simplifies the process of sending requests like GET, POST, PUT, DELETE, and more, and handling the responses. It is designed to be instantiated once and reused throughout the lifetime of an application. Repeatedly instantiating HttpClient can lead to socket exhaustion.
Key features and considerations:
- Efficiently handles HTTP connections.
- Supports asynchronous operations for non-blocking network calls.
- Provides methods for sending various HTTP methods.
- Allows customization of request headers, content, and timeouts.
- Implements
IDisposable, so it should be disposed of when no longer needed to release resources.
Constructors
| Name | Description |
|---|---|
| HttpClient() | Initializes a new instance of the HttpClient class with default values. |
| HttpClient(HttpMessageHandler) | Initializes a new instance of the HttpClient class with a specific message handler. |
| HttpClient(HttpMessageHandler, Boolean) | Initializes a new instance of the HttpClient class with a specific message handler and a value that indicates whether the handler should be disposed of. |
Methods
| Name | Description |
|---|---|
| DeleteAsync(...) | Send an HTTP DELETE request to the specified Uri as an asynchronous operation. |
| GetAsync(...) | Send an HTTP GET request to the specified Uri as an asynchronous operation. |
| GetFromUriBased(...) | Helper method to send GET requests. |
| GetStringAsync(...) | Send a GET request to the specified Uri and return the response body as a string as an asynchronous operation. |
| PostAsync(...) | Send an HTTP POST request to the specified Uri as an asynchronous operation. |
| PutAsync(...) | Send an HTTP PUT request to the specified Uri as an asynchronous operation. |
| SendAsync(...) | Sends an HTTP request as an asynchronous operation. |
Properties
| Name | Description |
|---|---|
| BaseAddress | Gets or sets the base address of Uniform Resource Identifiers (URIs) for network requests made by this instance. |
| DefaultRequestHeaders | Gets a set of HTTP header name-value pairs that will be sent with every request. |
| Timeout | Gets or sets the timespan to wait one attempt to send an asynchronous request to the server. |
Example
The following example shows how to use HttpClient to perform a simple GET request.
using System;
using System.Net.Http;
using System.Threading.Tasks;
public class Example
{
public static async Task Main(string[] args)
{
// It's recommended to reuse HttpClient for performance reasons
using (var client = new HttpClient())
{
try
{
// Make a GET request to a public API
HttpResponseMessage response = await client.GetAsync("https://jsonplaceholder.typicode.com/todos/1");
// Ensure the request was successful
response.EnsureSuccessStatusCode();
// Read the response content as a string
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
catch (HttpRequestException e)
{
Console.WriteLine($"\nException Caught: {e.Message}");
}
}
}
}