HttpClient和客户端证书错误

时间:2016-08-11 08:30:25

标签: c# dotnet-httpclient

我正在使用System.Net实现REST api调用...在ssl上使用HttpClient。我获得了客户证书。我将客户端证书存储在应用程序可执行文件的bin文件夹中(该应用程序是一个控制台应用程序。)因此,在我的代码中,我读取了这样的客户端证书:

    private X509Certificate2 GetClientCertificate()
    {
        try
        {
            return new X509Certificate2(File.ReadAllBytes(_certificateLocation),
                _certificatePassword);
        }
        catch (Exception) {
            return null;
        }
    }

我也忽略了SSL请求之前的错误:

        ServicePointManager.ServerCertificateValidationCallback += (sender,
            certificate,
            chain,
            errors) => true;

我使用以下代码发送http请求:

var handler = new WebRequestHandler();
var cert = GetClientCertificate();
 handler.ClientCertificates.Add(cert);
using (var client = new HttpClient(handler))
            {
                var resp = await client.PostAsync(new Uri(_apiEndPoint), content);
                resp.EnsureSuccessStatusCode();
.........
.........

但是我收到以下错误:

An error occurred while sending the request.:: The request was aborted: Could not create SSL/TLS secure channel.

有人可以帮助我吗?

0 个答案:

没有答案