【发布时间】:2019-12-18 15:07:06
【问题描述】:
HttpClient 的推荐是reuse a single instance。但从 API 看来,添加证书的方式似乎是在实例上,而不是每个请求。如果我们添加两个证书,我们如何确保“cert 1”只发送到“one.somedomain.com”?
//A handler is how you add client certs (is there any other way?)
var _clientHandler = new HttpClientHandler();
//Add multiple certs
_clientHandler.ClientCertificates.Add(cert1);
_clientHandler.ClientCertificates.Add(cert2);
_clientHandler.ClientCertificateOptions = ClientCertificateOption.Manual;
//Pretend this is our long-living HttpClient
var client = new HttpClient(_clientHandler);
//Now if we make a post request, will both certs be used?
using (HttpResponseMessage response = _client.PostAsync("https://one.somedomain.com", content).Result)
{
//...elided...
}
【问题讨论】:
-
看来,您需要以可取的行为实现自己的
HttpClientHandler。 -
我认为为每台服务器创建新的 HttpClient 可以节省一天的时间。您可以将它们放在字典中,其中键是服务器基地址
-
@KrivitskiyGrigoriy 这可能是唯一的选择,但我想知道这是否会在每个请求上增加太多额外的处理,或者是否有办法保持快速?另外,你知道怎么做吗?
标签: c# .net asp.net-core .net-core dotnet-httpclient