我已经实现了一种使用aps.net core(System.Net.HttpWebRequest
)的REST API请求(SSL基于一个:https://
)的响应的方法。
我需要忽略获得WebResponse
时发生的证书错误。我推荐了许多博客,并获得了使用ServicePointManager
的解决方案,并在下面使用了此信息
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
但是在ASP.NET核心中,不支持ServicePointManager
。因此,请帮助我仅通过HttpWebRequest
来解决此问题,而不是通过HttpClient
。
.net core使您可以使用servercertificatificatificatificatecustomvalidationcallback委托委托您可以覆盖。示例:
var handler = new HttpClientHandler();
handler.ServerCertificateCustomValidationCallback = delegate { return true; };
var http = new HttpClient(handler);
Console.WriteLine(http.GetAsync("https://expired.badssl.com/").GetAwaiter().GetResult());
输出:
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Server: nginx/1.10.0
Server: (Ubuntu)
Date: Mon, 06 Mar 2017 05:56:52 GMT
Transfer-Encoding: chunked
Connection: keep-alive
ETag: W/"58924b62-1d5"
Cache-Control: no-store
Content-Type: text/html
Last-Modified: Wed, 01 Feb 2017 20:56:02 GMT
}
在Ubuntu 16.04上测试了.NET 1.0.0-PREVIEW2-1-003177。在MacOS上有一个与此相同的开放问题。
在调用请求之前:
ServicePointManager.ServerCertificateValidationCallback = new
RemoteCertificateValidationCallback(validarCertificado);
...并包括此功能:
protected bool validarCertificado(Object sender,
X509Certificate certificado,
X509Chain cadena,
SslPolicyErrors sslErrores)
{
return true;
}