HttpClient Class
Assembly: System.Net.Http.dll
Summary
Provides a base class for sending HTTP requests and receiving HTTP responses from a resource identified by a URI. This class is thread-safe.
Constructors
| Name | Description | Syntax |
|---|---|---|
| HttpClient() | Initializes a new instance of the HttpClient class. |
public HttpClient(); |
| HttpClient(HttpMessageHandler) | Initializes a new instance of the HttpClient class with a specific handler. |
public HttpClient(HttpMessageHandler innerHandler); |
| HttpClient(HttpMessageHandler, Boolean) | Initializes a new instance of the HttpClient class with a specific handler and disposal option. |
public HttpClient(HttpMessageHandler innerHandler, bool disposeHandler); |
Methods
| Name | Description | Syntax |
|---|---|---|
| DeleteAsync(String) | Send a DELETE request to the specified URI. | public Task<HttpResponseMessage> DeleteAsync(string requestUri); |
| GetAsync(String) | Send a GET request to the specified URI. | public Task<HttpResponseMessage> GetAsync(string requestUri); |
| PostAsync(String, HttpContent) | Send a POST request to the specified URI. | public Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content); |
| PutAsync(String, HttpContent) | Send a PUT request to the specified URI. | public Task<HttpResponseMessage> PutAsync(string requestUri, HttpContent content); |
| SendAsync(HttpRequestMessage) | Send an HTTP request as an asynchronous operation. | public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request); |
| Dispose() | Releases the unmanaged resources and disposes of the managed resources used by the HttpClient. |
public void Dispose(); |
Properties
| Name | Description | Syntax |
|---|---|---|
| DefaultRequestHeaders | Gets or sets the default request headers for all requests sent by the client. | public HttpRequestHeaders DefaultRequestHeaders { get; } |
| BaseAddress | Gets or sets the base address of Uniform Resource Identifiers (URIs) for requests made by this instance. | public Uri BaseAddress { get; set; } |
Events
| Name | Description | Syntax |
|---|---|---|
| [`event` in C#] | No public events are exposed by this class. | N/A |
Remarks
The HttpClient class provides an abstraction over HTTP protocols. You can use it to send HTTP requests to a Web service and receive HTTP responses from it.
The HttpClient class is designed to be instantiated once, for the lifetime of an application, and reused. Instantiating an instance of HttpClient for each request will exhaust the number of sockets available under heavy loads. This can lead to performance problems and SocketException errors.
The best practice is to use a single instance of HttpClient for the lifetime of your application. For more information, see HttpClient lifecycle management.
HttpClient implements the IDisposable interface. When you are finished using the HttpClient object, you should call its Dispose method to release resources.
Example
The following example shows how to use the HttpClient class to download the content of a Web page.
using System;
using System.Net.Http;
using System.Threading.Tasks;
public class Example
{
public static async Task Main(string[] args)
{
using (HttpClient client = new HttpClient())
{
try
{
string url = "https://www.example.com";
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode(); // Throw if HTTP status is 4xx or 5xx
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
catch (HttpRequestException e)
{
Console.WriteLine("\nException Caught!");
Console.WriteLine("Message :{0} ", e.Message);
}
}
}
}