HttpClient One-Pager
Introduction
-
The
HttpClient
class sends http requests and receives http responses from a Uri. -
An
HttpClient
object instance is a collection of settings that is applied to all requests. -
Each object instance uses its own connection pool, which isolates its requests from the others.
HttpClient
-
The
HttpContent
type is used to represent an Http body and content headers. Use theHttpContent
class to specify the body of the request. It also represents the response body accessible on the HttpResponseMessage.Content property. See HttpContent -
Send an HttpPost request with a json body formatted as a string
-
Send an HttpPost request with a strongly typed object as Json
-
Send an HttpPut request with a json body formatted as a string
-
Send an HttpPut request with a strongly typed object as Json
-
Send an HttpPatch request with a json body formatted as a string
-
Send an HttpDelete request with a json body formatted as a string
-
The
Http Head
request returns only the headers associated with a resource. A response to theHead
request does not return a body. Http Head -
The
Http Options
request is used to identify which Http methods a server or endpoint supports. Http Options
HttpResponseMessage
-
The
HttpResponseMessage
type contains the HttpResponse. -
You can access the response body using the
Content
property. Handling valid Http Content responses
HttpRequestException
-
HttpRequestException
is thrown when an HttpRequest fails. Handling an HttpRequestException
HttpProxy
-
A proxy can be configured in 2 ways.
-
A default is specified on the
HttpClient.DefaultProxy
property. -
Or you can specify the
HttpClientHandler.Proxy
property.
-
-
The
HttpClient.DefaultProxy
is a static property that determines the default proxy that allHttpClient
instances use. See Global default proxy
IHttpClientFactory
-
IHttpClientFactory
serves as an abstraction that can createHttpClient
instances with custom configurations. -
If your app uses cookies then it is better to avoid
IHttpClientFactory
. -
Use
IHttpClientFactory
for short-lived clients. UsePooledConnectionLifetime
for long-lived clients. -
To regiser
IHttpClientFactory
, callAddHttpClient
. See Basic Usage -
Use named clients, if your app needs to connect to multiple services. See Named Clients
-
Typed clients are the same as named clients but without the use of strings. Probably better to use this. See Typed Clients
-
Get, post, put method calls are no different from
HttpClient
. See Get, Post, Put
HttpClient lifetime Management
-
IHttpClientFactory
creates a new instance ofHttpClient
for every call toCreateClient
. -
One
HttpClientHandler
is created per client name. -
The factory manages the lifetimes of the
HttpClientHandler
instances. -
To reduce resource consumption,
IHttpClientFactory
cachesHttpClientHandler
instances. -
An
HttpClientHandler
instance may be reused from the cache if its lifetime has not expired. -
Caching of handlers is desirable as each handler manages its own underlying connection pool.
-
Creating more handlers may lead to socket exhaustion and connection delays.
-
Some handlers may also keep the connection open indefinitely, which can prevent the handler reacting to DNS changes.
-
The default handler lifetime is two minutes.
-
You can change the lifetime value. see HttpClient Lifetime Management.
SocketsHttpHandler
-
The
SocketsHttpHandler
implementation ofHttpMessageHandler
allowsPooledConnectionLifetime
to be configured. -
This setting is used to ensure that the handler reacts to DNS changes, so using
SocketsHttpHandler
is an alternative to usingIHttpClientFactory
. -
IHttpClientFactory
andSocketsHttpHandler
can be used together. See the details
Use named clients in singleton service.
-
With named clients,
IHttpClientFactory
is injected into services, a newHttpClient
is created everytimeCreateClient
is called. -
Typed clients are transient objects usually injected into services. Typed clients are supposed to be short-lived. Singleton services are supposed to be long-lived. See the problem there.
Message Handler DI Scope
-
IHttpClientFactory
creates a seperate DI scope per eachHttpMessageHandler
instance. -
They are separate from application DI scope like aspnetcore request scope or user-created manual DI scope.
-
HttpMessageHandler scopes can outlive application scopes.
-
Do not cache scope related information inside HttpMessageHandler instances.
-
Generally avoid such code gymnastics. Use this section more like an fyi.
Guidelines for using HttpClient
-
If a DNS entry changes regularly, the client wont respect those updates. Limit the lifetime of the connection by setting the PooledConnectionLifetime property, so that DNS lookup is repeated when the connection is replaced. For more information
-
To avoid port exhaustion problems, reusing
HttpClient
instance for as many Http requests as possible is recommended. Recommended usage
Rate limit an Http handler
-
To use rate limiting in .Net, add a reference to
System.Threading.RateLimiting
nuget package. -
There is now a rate limiting middleware option. In case there is a rate limiting requirment for your application, click here for a sample implementation of how to set it up.
Http/3 support
-
In case, you really want to use Http/3. I would not recommend using it at this point of time.