我在使用 http 客户端的自定义证书进行 HTTP 调用时遇到问题。例子:
try
{
var handler = new HttpClientHandler();
//download certificate from https://api.alfabank.ru/sites/api.alfabank.ru/files/u1/apidevelopers.cert.pem
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "apidevelopers.cert.pem");
handler.ClientCertificates.Add(new X509Certificate2(path));
using (var httpClient = new HttpClient(handler))
{
var response =
await httpClient.GetAsync(
"https://apiws.alfabank.ru");
Console.WriteLine(response.StatusCode);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
if (ex.InnerException != null)
{
Console.WriteLine(ex.InnerException.Message);
Console.WriteLine(ex.InnerException.StackTrace);
}
}
Run Code Online (Sandbox Code Playgroud)
发生的错误是:
System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception. ---> System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception. ---> System.ComponentModel.Win32Exception: Unexpected message received or incorrect …Run Code Online (Sandbox Code Playgroud)