首页 > 解决方案 > 请求被中止:无法创建 SSL/TLS 安全通道,但正在使用 Postman / CURL

问题描述

我正在拼命地尝试与使用.NET 的服务器进行通信。

我的代码相对简单,我使用的是来自 .NET Core 的最新 httpClient 类和 Framework 4.7.2(Windows 10 版本 1903):

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            ServicePointManager.ServerCertificateValidationCallback +=
                (sender, cert, chain, sslPolicyErrors) => true;

            using (var httpClient = new HttpClient())
            {
                using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://oauth2-apiii.e-i.com"))
                {
                    request.Headers.TryAddWithoutValidation("Accept", "*/*");
                    
                    var contentList = new List<string>();
                    contentList.Add($"grant_type={Uri.EscapeDataString("authorization_code")}");
                    contentList.Add($"code={Uri.EscapeDataString("a")}");
                    contentList.Add($"scope={Uri.EscapeDataString("b")}");
                    contentList.Add($"client_id={Uri.EscapeDataString("c")}");
                    contentList.Add($"code_verifier={Uri.EscapeDataString("d")}");
                    request.Content = new StringContent(string.Join("&", contentList));
                    request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");

                    var response = httpClient.SendAsync(request).GetAwaiter().GetResult();
                    HttpContent httpContent = response.Content;
                    string test = httpContent.ToString();
                }
            }

        }
    }
}

我只收到以下错误:请求被中止:无法创建 SSL/TLS 安全通道。

我尝试了其他方法(HttpRequest,RestSharp),但它仍然不起作用。

我尝试使用 cURL 和 Postman,结果还可以,我从服务器得到了正确的结果。

cURL/Postman 怎么可能设法向这个 URL 发送请求,而 .NET 却不能呢?

编辑:

这是 Fiddler .NET 隧道跟踪:

[Fiddler .NET 隧道 trace1

这是 Fiddler Postman 隧道跟踪:

[提琴手邮递员隧道跟踪

我看不到明显的区别,似乎双方使用的密码相同:TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384

编辑 2:有趣的事实:Internet Explorer 和 Microsoft Edge(不是 Chromium)都无法访问 URL:https ://oauth2-apiii.ei.com/sandbox/cic/oauth2而 Firefox、Edge Chromium 和 Chrome 没有困难.

我想这个问题可能是相关的。

标签: c#ssl

解决方案


推荐阅读