HttpClient Class
Provides a base class for sending HTTP requests and receiving HTTP responses from a resource identified by a URI. This is a modern, efficient, and flexible class for making HTTP requests in .NET.
HttpClient is designed to be instantiated once and re-used throughout the life of an application. Instantiating an instance of HttpClient for every request will exhaust the number of sockets available under heavy loads. This can lead to SocketException errors.
Constructors
-
HttpClient()
Initializes a new instance of the
HttpClientclass with the default constructor. -
HttpClient(HttpMessageHandler handler)
Initializes a new instance of the
HttpClientclass with a specificHttpMessageHandler.handler- The HTTP handler to handle this HTTP client.
-
HttpClient(HttpMessageHandler handler, bool disposeHandler)
Initializes a new instance of the
HttpClientclass with a specificHttpMessageHandlerand a value that indicates whether the handler should be disposed of.handler- The HTTP handler to handle this HTTP client.
disposeHandlertrueto dispose of the handler; otherwise,false.
Methods
-
GetAsync(string requestUri)
Send a GET request to the specified Uri.
requestUri- The Uri to request.
Task<HttpResponseMessage>- The task object representing the asynchronous operation.
Example
using System.Net.Http; using System.Threading.Tasks; public async Task<string> GetApiDataAsync(string url) { using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync(url); response.EnsureSuccessStatusCode(); // Throws an exception if the status code is an error string responseBody = await response.Content.ReadAsStringAsync(); return responseBody; } } -
PostAsync(string requestUri, HttpContent content)
Send a POST request to the specified Uri.
requestUri- The Uri to request.
content- The HTTP content to send with the request.
Task<HttpResponseMessage>- The task object representing the asynchronous operation.
-
PutAsync(string requestUri, HttpContent content)
Send a PUT request to the specified Uri.
requestUri- The Uri to request.
content- The HTTP content to send with the request.
Task<HttpResponseMessage>- The task object representing the asynchronous operation.
-
DeleteAsync(string requestUri)
Send a DELETE request to the specified Uri.
requestUri- The Uri to request.
Task<HttpResponseMessage>- The task object representing the asynchronous operation.
-
SendAsync(HttpRequestMessage request)
Send an HTTP request as an asynchronous operation.
request- The HTTP request message to send.
Task<HttpResponseMessage>- The task object representing the asynchronous operation.
-
Dispose()
Releases the unmanaged resources used by the
HttpClientand optionally disposes of the managed resources.
Properties
-
BaseAddress
Gets or sets the base address of Uniform Resource Identifier (URI) of the Internet resource that the client references.
Uri
-
DefaultRequestHeaders
Gets or sets the default request headers for all requests made by this
HttpClient.HttpRequestHeaders
-
MaxResponseContentBufferSize
Gets or sets the maximum buffer size in bytes to use when reading the content of a response.
long
-
Timeout
Gets or sets the timespan to wait one or more synchronous operations to complete.
TimeSpan