In C#, HttpClient
is a class provided by the System.Net.Http
namespace that is used for sending HTTP requests and receiving HTTP responses from a resource identified by a URI. The HttpClient
class provides several methods for sending HTTP requests. Here are some of the most commonly used methods:
- GetAsync: Sends an HTTP GET request to the specified URI and returns the response body as a string.
public Task<HttpResponseMessage> GetAsync(string requestUri)
public Task<HttpResponseMessage> GetAsync(string requestUri, HttpCompletionOption completionOption)
public Task<HttpResponseMessage> GetAsync(string requestUri, HttpCompletionOption completionOption, CancellationToken cancellationToken)
public Task<HttpResponseMessage> GetAsync(Uri requestUri, HttpCompletionOption completionOption)
- PostAsync: Sends an HTTP POST request to the specified URI with the provided HTTP content.
public Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content)
public Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content, CancellationToken cancellationToken)
- PutAsync: Sends an HTTP PUT request to the specified URI with the provided HTTP content.
public Task<HttpResponseMessage> PutAsync(string requestUri, HttpContent content)
public Task<HttpResponseMessage> PutAsync(string requestUri, HttpContent content, CancellationToken cancellationToken)
- DeleteAsync: Sends an HTTP DELETE request to the specified URI.
public Task<HttpResponseMessage> DeleteAsync(string requestUri)
public Task<HttpResponseMessage> DeleteAsync(string requestUri, CancellationToken cancellationToken)
- SendAsync: Sends an HTTP request with the specified HttpRequestMessage and returns the response. This method is more generic and can be used for any HTTP method (GET, POST, PUT, DELETE, etc.) by specifying the HTTP method in the HttpRequestMessage.
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request)
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption)
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
- PatchAsync: Sends an HTTP PATCH request to the specified URI with the provided HTTP content. Patching is used to apply partial modifications to a resource.
public Task<HttpResponseMessage> PatchAsync(string requestUri, HttpContent content)
public Task<HttpResponseMessage> PatchAsync(string requestUri, HttpContent content, CancellationToken cancellationToken)
Here's an example of using HttpClient
to send a GET request:
using System;
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
static readonly HttpClient client = new HttpClient();
static async Task Main()
{
try
{
HttpResponseMessage response = await client.GetAsync("http://example.com");
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
catch (HttpRequestException e)
{
Console.WriteLine("\nException Caught!");
Console.WriteLine("Message :{0} ", e.Message);
}
}
}
When using HttpClient
, it is recommended to create a single instance and reuse it throughout the life of an application, rather than creating a new instance for each request. This can improve the performance of your application by reusing the underlying connection pool.
Remember to include error handling in your code when working with HttpClient
. The example above uses EnsureSuccessStatusCode
to throw an exception if the HTTP response status is not successful (2xx). You should also handle other exceptions that might occur, such as HttpRequestException
.