System.Net.Http.HttpClient ClassHttpClient Class
Namespace: System.Net.Http

HttpClient Class

Provides a base class for sending HTTP requests and receiving HTTP responses from a resource identified over a network.

Syntax

public class HttpClient : IDisposable

Remarks

The HttpClient class is a fundamental component for making HTTP requests in .NET applications. It simplifies the process of sending requests like GET, POST, PUT, DELETE, etc., and handling the responses.

It is recommended to use a single instance of HttpClient for the lifetime of an application. Creating an instance per request can lead to socket exhaustion due to the underlying connection pooling mechanisms.

For more details on best practices and common usage patterns, refer to the Microsoft Docs HttpClient Guidance.

Properties

Name Description
DefaultRequestHeaders Gets a collection of HTTP header‐value pairs to send with every request.
BaseAddress Gets or sets the base uniform resource identifier (URI) of a request.

Methods

Name Description
GetAsync(String requestUri) Send a GET request to the specified Uri as an asynchronous operation.
PostAsync(String requestUri, HttpContent content) Send a POST request to the specified Uri as an asynchronous operation.
PutAsync(String requestUri, HttpContent content) Send a PUT request to the specified Uri as an asynchronous operation.
DeleteAsync(String requestUri) Send a DELETE request to the specified Uri as an asynchronous operation.
SendAsync(HttpRequestMessage request) Send an HTTP request as an asynchronous operation.
Dispose() Releases the unmanaged resources used by the System.Net.Http.HttpClient and optionally releases the managed resources.

Example

Making a GET Request

This example demonstrates how to create an HttpClient instance, make a GET request to a sample API, and read the response.

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 a single HttpClient instance for the lifetime of the application. using (HttpClient client = new HttpClient()) { try { // Send 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("Response:"); Console.WriteLine(responseBody); } catch (HttpRequestException e) { Console.WriteLine($"Request error: {e.Message}"); } catch (Exception ex) { Console.WriteLine($"An unexpected error occurred: {ex.Message}"); } } } }

Example Output

Response: { "userId": 1, "id": 1, "title": "delectus aut autem", "completed": false }

Making a POST Request

This example shows how to send a POST request with JSON content.

using System; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; public class PostExample { public static async Task Main(string[] args) { using (HttpClient client = new HttpClient()) { try { var jsonPayload = "{\"title\": \"foo\", \"body\": \"bar\", \"userId\": 1}"; var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync("https://jsonplaceholder.typicode.com/posts", content); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine("Response:"); Console.WriteLine(responseBody); } catch (HttpRequestException e) { Console.WriteLine($"Request error: {e.Message}"); } } } }

See Also

< Previous: System.Net.Http Namespace Next: HttpResponseMessage >