HttpMessageHandler Configuration
HttpMessageHandler Configuration
HttpMessageHandler is the core component of HttpClient responsible for processing HTTP requests and responses. By configuring HttpMessageHandler, you can implement advanced features such as proxy, certificates, and cookies.
HTTP Proxy Configuration
services.AddHttpApi<IUserApi>().ConfigureHttpApi(o =>
{
o.HttpHost = new Uri("http://localhost:5000/");
})
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
{
UseProxy = true,
Proxy = new WebProxy
{
Address = new Uri("http://proxy.com"),
Credentials = new NetworkCredential
{
UserName = "username",
Password = "password"
}
}
});Client Certificate Configuration
Some servers enable HTTPS mutual authentication to restrict client connections, only allowing clients with specific certificates to connect:
services.AddHttpApi<IUserApi>().ConfigureHttpApi(o =>
{
o.HttpHost = new Uri("https://localhost:5001/");
})
.ConfigurePrimaryHttpMessageHandler(() =>
{
var handler = new HttpClientHandler();
handler.ClientCertificates.Add(yourCert);
return handler;
});Maintaining CookieContainer Persistence
If the API uses cookies to store authentication information, you need to maintain the CookieContainer instance independently so it does not follow the HttpMessageHandler lifecycle. The default HttpMessageHandler has a minimum lifecycle of only 2 minutes.
var cookieContainer = new CookieContainer();
services.AddHttpApi<IUserApi>().ConfigureHttpApi(o =>
{
o.HttpHost = new Uri("http://localhost:5000/");
})
.ConfigurePrimaryHttpMessageHandler(() =>
{
var handler = new HttpClientHandler();
handler.CookieContainer = cookieContainer;
return handler;
});Skip SSL Certificate Validation (Development Only)
services.AddHttpApi<IUserApi>().ConfigurePrimaryHttpMessageHandler(() =>
{
var handler = new HttpClientHandler
{
ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true
};
return handler;
});Warning
Skipping SSL certificate validation should only be used in development environments. Production environments must use valid SSL certificates.
Adding Custom DelegatingHandler
services.AddHttpApi<IUserApi>()
.AddHttpMessageHandler(() => new CustomDelegatingHandler());
public class CustomDelegatingHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
// Pre-request processing
request.Headers.Add("X-Request-Id", Guid.NewGuid().ToString());
var response = await base.SendAsync(request, cancellationToken);
// Post-response processing
return response;
}
}