繁体   English   中英

HttpClient无法与HTTPs端点握手

[英]HttpClient unable to handshake with an HTTPs endpoint

我需要从WebAPI控制器内部的API端点获取数据。 我正在使用HttpClient实现目标。 但是,客户端似乎无法从端点获取数据。 connect调用失败,并显示以下错误:

 HTTPS handshake to test.site failed. System.IO.IOException Unable to read  
 data from the transport connection: An existing connection was forcibly  
 closed by the remote host. <An existing connection was forcibly closed by 
 the remote host>

使用有效的SSL证书正确托管API端点。 此外,如果我使用控制台应用程序/使用邮递员访问API,则没有错误,并且可以看到响应。 但是,当我尝试从API控制器内部以完全相同的方式访问相同的API时,会看到SSL连接错误。

这是客户端的代码:

HttpClient client = new HttpClient()
        {
            BaseAddress = new Uri(uri)
        };

var requestMessage = new HttpRequestMessage(                                
                         new HttpMethod("GET"),
                         requestUrl);

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

client.DefaultRequestHeaders.Add(
                "User-Agent",
                "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));

var responseMessage = await client.SendAsync(requestMessage);

与控制台应用程序相比,为什么WebAPI发出的connect请求的行为不同? 您能帮我解决以上问题吗?

.NET框架的较旧版本默认使用较旧的TLS,而TLS可能会被端点拒绝。 拒绝使用低于TLS v1.2的请求已成为标准。

要解决此问题,您可以使用.NET框架的较新版本,也可以显式设置ServicePointManager使用的TLS版本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM