HttpClientHandler
HttpClient configuration is done using a HttpClientHandler - configure it with the UseHttpClientHandler extension method, which gives you the HttpClientHandler RestClient will use so you can set any of its properties: proxies, credentials, automatic decompression, redirects, client certificates, server certificate validation and so on.
More information on HttpClientHandler
Example routing requests through a proxy and enabling automatic decompression:
var config = new Config()
.UseHttpClientHandler(handler =>
{
handler.Proxy = new WebProxy("http://proxy.mycompany.com:8080");
handler.UseProxy = true;
handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
});
dynamic restClient = new RestClient("https://jsonplaceholder.typicode.com", config);
When using IHttpClientFactory use the same UseHttpClientHandler extension method when adding the RestClient to your DI container:
services
.AddRestClient("https://jsonplaceholder.typicode.com")
.UseHttpClientHandler(handler =>
{
handler.Proxy = new WebProxy("http://proxy.mycompany.com:8080");
handler.UseProxy = true;
});
Note you can only add one HttpClientHandler to the pipeline. The overloads taking a HttpClientHandler instance or Func<HttpClientHandler> are obsolete - use the Action<HttpClientHandler> overloads shown above.
For cookies use the CookieHandler - it configures the HttpClientHandler’s CookieContainer for you and makes the cookies available on the response.