You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Framework 4.7.2调用API遇SSL/TLS安全通道创建失败求助

ASP.NET MVC调用外部API出现SSL/TLS安全通道创建失败问题

在基于.NET Framework 4.7.2的ASP.NET MVC项目中,调用部署于其他服务器的供应商API时,持续报错:The request was aborted: Could not create SSL/TLS secure channel。但在部署项目的Windows 2019服务器上通过SOAP UI调用该API可正常获取响应。

先后尝试使用RestClient、HttpWebRequest、HttpClient三种方式编写调用代码,均在本地运行正常,但部署到服务器后出现上述错误。相关代码如下:

RestClient调用代码

try
{
    var client = new RestClient(url);
    client.Timeout = -1;
    var request = new RestRequest(Method.POST);
    
    request.AddHeader("Authorization", "Basic " + XXXXXXX);
    request.AddParameter("grant_type", "XXXXXXXX");
    
    //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

    ServicePointManager.Expect100Continue = true;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
           | SecurityProtocolType.Tls11
           | SecurityProtocolType.Tls12
           | SecurityProtocolType.Ssl3;
    ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

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

    IRestResponse response = client.Execute(request);
    return (response.Content);
}
catch (Exception ex)
{
    throw ex;
}

更新内容:HttpWebRequest调用代码

使用HttpWebRequest编写的新代码仍出现相同错误,代码如下:

try
{
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

    //ServicePointManager.Expect100Continue = true;
    //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
    //                                       | SecurityProtocolType.Tls11
    //                                       | SecurityProtocolType.Tls12
    //                                       | SecurityProtocolType.Ssl3;
    //ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

    HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
    webRequest.Method = "POST";
    //webRequest.Proxy = new WebProxy(ConfigVariables.ProxyIP, ConfigVariables.ProxyPort);
    webRequest.ContentLength = postString.Length;

    if (!string.IsNullOrEmpty(headervalue))
    {
        webRequest.Headers.Add("Authorization", "Basic " + headervalue);
    }

    System.IO.StreamWriter requestWriter = new System.IO.StreamWriter(webRequest.GetRequestStream());
    requestWriter.Write(postString);
    requestWriter.Close();
    System.IO.StreamReader responseReader = new System.IO.StreamReader(webRequest.GetResponse().GetResponseStream());
    result = responseReader.ReadToEnd();

    responseReader.Close();
    webRequest.GetResponse().Close();
}
catch (Exception ex)
{
    throw ex;
}

更新内容2:HttpClient调用代码

尝试使用HttpClient仍存在相同问题,代码如下:

try
{
    using (var client = new System.Net.Http.HttpClient())
    {
        //Passing service base url
        //client.BaseAddress = new Uri(url);
        client.DefaultRequestHeaders.Clear();
        //Define request data format
        client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", headervalue);
        //Sending request to web api
        System.Net.Http.HttpRequestMessage request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Post, url);
        //Receiving  response from web api
        System.Net.Http.HttpResponseMessage response = client.SendAsync(request).GetAwaiter().GetResult();

        //Checking the response is successful or not which is sent using HttpClient
        if (response.IsSuccessStatusCode)
        {
            //Storing the response details recieved from web api
            result = response.Content.ReadAsStringAsync().Result;
        }
    }
}
catch (Exception ex)
{
    throw ex;
}

附SSLLabs的API报告截图:
SSLLabs API报告截图

请问问题出在哪里?


内容的提问来源于stack exchange,提问作者Rahul More

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 01:03:19