public static IHttpClientBuilder UseHttpClient(this ICoreBuilder clientBuilder, IOptions <HttpClientConfig> options = null) { CustomHttpClientBuilder builder = new CustomHttpClientBuilder(clientBuilder.Services); builder.Services.AddSingleton(x => { HttpClientHandler handler = new HttpClientHandler(); handler.UseCookies = false; handler.MaxAutomaticRedirections = 3; handler.SslProtocols = SslProtocols.None; //Let the OS handle the protocol to use handler.UseProxy = options.Value.UseProxy; handler.Proxy = options.Value.Proxy; return(handler); }); builder.Services.AddSingleton(serviceProvider => { System.Net.Http.HttpClient client = ActivatorUtilities.CreateInstance <System.Net.Http.HttpClient>(serviceProvider); client.DefaultRequestHeaders.TransferEncodingChunked = false; return(client); }); builder.Services.AddSingleton <INetworkDriver, HttpClientNetworkDriver>(); return(builder); }
public static IHttpClientBuilder UseHttpClient(this ICoreBuilder clientBuilder) { CustomHttpClientBuilder builder = new CustomHttpClientBuilder(clientBuilder.Services); builder.Services.AddSingleton <INetworkDriver, HttpClientNetworkDriver>(x => { HttpClientHandler handler = new HttpClientHandler(); handler.UseCookies = false; handler.MaxAutomaticRedirections = 3; handler.SslProtocols = SslProtocols.None; //Let the OS handle the protocol to use IOptions <HttpClientConfig> options = x.GetService <IOptions <HttpClientConfig> >(); if (options != null) { handler.UseProxy = options.Value.UseProxy; handler.Proxy = options.Value.Proxy; } ILogger <HttpClientNetworkDriver> logger = x.GetRequiredService <ILogger <HttpClientNetworkDriver> >(); System.Net.Http.HttpClient client = new System.Net.Http.HttpClient(handler); client.DefaultRequestHeaders.UserAgent.TryParseAdd(Constants.DefaultUserAgent); client.DefaultRequestHeaders.TransferEncodingChunked = false; return(new HttpClientNetworkDriver(logger, client)); }); return(builder); }