Tag: httpclientfactory

将httpHandler附加到httpclientFactory webapi aspnetcore 2.1

我试图使用“ConfigurePrimaryHttpMessageHandler”将处理程序附加到httpclientfactory 但当我查看HttpClient内部以查看处理程序是否存在时,我无法找到它 我是否正确附加了处理程序? 任何建议 services.AddHttpClient(client => { client.BaseAddress = new Uri(myUri); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(“application/json”)); }) .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { AllowAutoRedirect = false, AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip }); public interface IGitHubClient { Task GetData(); } public class GitHubClient : IGitHubClient { private readonly HttpClient _client; public GitHubClient(HttpClient httpClient) { _client = httpClient; } public async […]

配置HttpClientFactory时出现证书错误

我需要在HttpClientFactory中添加证书。 使用HttpClient旧实现看起来像这样: var cookieContainer = new CookieContainer(); var handler = new HttpClientHandler { CookieContainer = cookieContainer }; var basePath = Directory.GetCurrentDirectory(); var certificatePath = Path.Combine(basePath, certPath); var fileExists = File.Exists(certificatePath); if (!fileExists) throw new ArgumentException(certificatePath); var certificate = new X509Certificate2(certificatePath, certPwd); handler.ClientCertificates.Add(certificate); using (var client = new HttpClient(handler)) { client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(accept)); client.DefaultRequestHeaders.Add(“ApiKey”, apiKey); var body […]

如何在.NET Core中使用HttpClientHandler和HttpClientFactory

我想使用.NET Core 2.1中提供的HttpClientFactory ,但我还想在创建HttpClients时使用HttpClientHandler来利用AutomaticDecompression属性。 我很挣扎,因为.AddHttpMessageHandler接受DelegatingHandler而不是HttpClientHandler 。 有谁知道如何使这个工作? 谢谢,吉姆