HttpClientHandlerOptions Class
Declaration:
public class HttpClientHandlerOptions
Inheritance:System.Object
System.Net.Http.HttpClientHandlerOptions
Implemented interfaces: None
Remarks
Provides options for configuring the behavior of an HttpClientHandler. This class allows fine-grained control over aspects like SSL/TLS certificate validation, proxy settings, cookie handling, and more.
Constructors
HttpClientHandlerOptions()
Initializes a new instance of the HttpClientHandlerOptions
class with default settings.
Properties
ClientCertificates
Gets or sets a collection of client certificates that are included in the request.
public System.Security.Cryptography.X509Certificates.X509CertificateCollection ClientCertificates { get; set; }
CookieContainer
Gets or sets the container that browsers use to hold cookies.
public System.Net.CookieContainer CookieContainer { get; set; }
MaxAutomaticRedirections
Gets or sets the maximum number of automatic redirections.
public int MaxAutomaticRedirections { get; set; }
MaxRequestContentBufferSize
Gets or sets the maximum size of the request content buffer.
public long MaxRequestContentBufferSize { get; set; }
PreAuthenticate
Gets or sets a value that indicates whether the handler performs automatic HTTP authentication.
public bool PreAuthenticate { get; set; }
Proxy
Gets or sets the proxy used to send requests.
public System.Net.IWebProxy Proxy { get; set; }
UseCookies
Gets or sets a value that indicates whether the handler uses cookies.
public bool UseCookies { get; set; }
UseProxy
Gets or sets a value that indicates whether the handler uses the proxy.
public bool UseProxy { get; set; }
SslOptions
Gets or sets options related to SSL/TLS configuration.
public System.Net.Security.SslClientAuthenticationOptions SslOptions { get; set; }
Example
Configuring a HttpClientHandler with custom options
using System;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography.X509Certificates;
public class Example
{
public static void Main(string[] args)
{
// Create a custom cookie container
var cookieContainer = new CookieContainer();
cookieContainer.Add(new Uri("http://example.com"), new Cookie("mycookie", "myvalue"));
// Configure SSL options
var sslOptions = new SslClientAuthenticationOptions
{
ClientCertificates = new X509CertificateCollection(), // Add client certificates if needed
RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => {
// Implement custom certificate validation logic here
// For example, to ignore all certificate errors (not recommended for production):
// return true;
return sslPolicyErrors == System.Net.Security.SslPolicyErrors.None;
}
};
// Configure the handler options
var handlerOptions = new HttpClientHandlerOptions
{
CookieContainer = cookieContainer,
UseCookies = true,
MaxAutomaticRedirections = 5,
Proxy = new WebProxy("http://myproxy.com:8888"), // Optional proxy
UseProxy = true,
SslOptions = sslOptions
};
// Create an HttpClientHandler with the configured options
var httpClientHandler = new HttpClientHandler
{
CookieContainer = handlerOptions.CookieContainer,
UseCookies = handlerOptions.UseCookies,
MaxAutomaticRedirections = handlerOptions.MaxAutomaticRedirections,
Proxy = handlerOptions.Proxy,
UseProxy = handlerOptions.UseProxy,
SslOptions = handlerOptions.SslOptions
};
// Use the HttpClient with the custom handler
using (var httpClient = new HttpClient(httpClientHandler))
{
try
{
HttpResponseMessage response = httpClient.GetAsync("https://example.com/api/data").Result;
response.EnsureSuccessStatusCode();
string responseBody = response.Content.ReadAsStringAsync().Result;
Console.WriteLine(responseBody);
}
catch (HttpRequestException e)
{
Console.WriteLine($"Request error: {e.Message}");
}
}
}
}
See Also
- HttpClientHandler class
- SslClientAuthenticationOptions class
- CookieContainer class