炫意html5
最早CSS3和HTML5移动技术网站之一

HttpClient 发送certificates,但HttpContext.Connection.ClientCertificate是空

使用HttpClient发送带有certificates的请求后,无法在api端获取。
客户端
var cert = new X509Certificate2(@”D:\upload\images\证书.pfx”, “123”);
var handler = new HttpClientHandler() { ClientCertificateOptions = ClientCertificateOption.Manual };
handler.ClientCertificates.Add(cert);

        var client = new HttpClient(handler);
var request = new HttpRequestMessage()
{
RequestUri = new Uri("https://localhost:44314/home/get"),
Method = HttpMethod.Get,
};
var response = client.SendAsync(request).GetAwaiter().GetResult();
if (response.IsSuccessStatusCode)
{
var responseContent = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
}

接收
var clientCertInRequest = Request.HttpContext.Connection.ClientCertificate;
clientCertInRequest 一直是空

回答

参考代码:
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
CloseableHttpClient httpClient = (CloseableHttpClient) requestFactory.getHttpClient();

TrustStrategy acceptingTrustStrategy = (cert, authType) -> true;
SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, ALLOW_ALL_HOSTNAME_VERIFIER);
httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 8443, sf));

ResponseEntity<String> response = new RestTemplate(requestFactory).
  exchange(urlOverHttps, HttpMethod.GET, null, String.class);

炫意HTML5 » HttpClient 发送certificates,但HttpContext.Connection.ClientCertificate是空

Java基础教程Android基础教程