代码之家  ›  专栏  ›  技术社区  ›  letsc

HttpClient无法与HTTPs端点握手

  •  0
  • letsc  · 技术社区  · 7 年前

    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>
    

    API端点使用有效的SSL证书正确承载。此外,如果我使用控制台应用程序/使用邮递员点击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的请求是什么?你能帮我解决上面的问题吗?

    1 回复  |  直到 7 年前
        1
  •  2
  •   jpishko    7 年前

    较旧版本的.NET framework默认使用较旧版本的TLS,这可能会被终结点拒绝。拒绝使用小于TLSV1.2的请求已成为标准。

    ServicePointManager 使用。