小编Vit*_*liy的帖子

异常 HttpRequestException -> AuthenticationException。无法建立 SSL 连接

我在使用 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)

c# ssl httpclient x509certificate2 .net-core

5
推荐指数
0
解决办法
3689
查看次数

标签 统计

.net-core ×1

c# ×1

httpclient ×1

ssl ×1

x509certificate2 ×1