HttpClientHandler / 请求被中止:无法创建 SSL/TLS 安全通道

Posted

技术标签:

【中文标题】HttpClientHandler / 请求被中止:无法创建 SSL/TLS 安全通道【英文标题】:HttpClientHandler / The request was aborted: Could not create SSL/TLS secure channel 【发布时间】:2013-10-25 13:32:02 【问题描述】:

服务器团队表示此 HTTPS 网站一切正常,但以下代码在使用 HTTPClient 时返回“请求中止:无法创建 SSL/TLS 安全通道。”错误和同时通过返回预期的数据工作得很好。

public async Task<string> GetDataAsync(string baseAddress, string relativeAddress, string token)

    string responseBodyAsText = string.Empty;
    string responseStatusCode = string.Empty;
    try
    
        using (HttpClientHandler handler = new HttpClientHandler())
        
            handler.Credentials = new NetworkCredential(token, token);
            using (HttpClient client = new HttpClient(handler))
            
                client.BaseAddress = new Uri(baseAddress);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
                using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, relativeAddress))
                
                    using (HttpResponseMessage response = await client.SendAsync(request))
                    
                        responseBodyAsText = await response.Content.ReadAsStringAsync();
                        responseStatusCode = ReturnStatusCode(response);
                    
                
            
        
    
    catch (Exception e)
    
        responseBodyAsText = BuildErrorMessage("response", "error", e.InnerException.Message, responseStatusCode);
    
    return responseBodyAsText;

为了以不同的方式解决这个问题,我编写了另一个使用 HTTPWebRequest 和 HTTPWebResponse 的方法。这段代码直到现在才给我这个错误“请求被中止:无法创建 SSL/TLS 安全通道。”;但是我想发布这个来找出可能的问题?

    public async Task<string> GetDataAsync(string url, string token)
    
        string responseString = string.Empty;
        try
        
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url.Trim());
            request.Method = "GET";
            request.ContentType = "application/xml";
            request.Headers[HttpRequestHeader.Authorization] = GetAuthorizationText(token.Trim());

            var response = (HttpWebResponse)await request.GetResponseAsync();
            if (response != null)
            
                var responseStream = new StreamReader(response.GetResponseStream());
                responseString = await responseStream.ReadToEndAsync();
                if (responseString.Trim().Length == 0)
                    throw new InvalidDataException("No Content");
            
            else
                throw new NullReferenceException("Reponse is null");
        
        catch (WebException we)
        
            responseString = BuildErrorMessage("response", "error", we.Response.Headers["Status"].ToString());
        
        catch (Exception ex)
        
            WriteDiagnosticsInformation(ex);
            responseString = BuildErrorMessage("response", "error", ex.Message);
        

        return responseString;
    

请分享您的想法,但我怀疑这可能是使用 HTTPClient 的问题。

【问题讨论】:

【参考方案1】:

试着把这个

ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)  return true; ;

在一切之前

【讨论】:

我最近没有收到上面提到的。如果您能告诉我这段代码的用途,那就太好了。谢谢!

以上是关于HttpClientHandler / 请求被中止:无法创建 SSL/TLS 安全通道的主要内容,如果未能解决你的问题,请参考以下文章

请求被中止:请求被取消。没有解决方案有效

System.Net.WebException:请求被中止:请求被取消

“请求被中止:无法创建 SSL/TLS 安全通道”

请求被中止: 未能创建 SSL/TLS 安全通道

发出请求时出错:请求被中止:无法创建 SSL/TLS 安全通道

请求被中止:无法为 HttpWebRequest 创建 SSL/TLS 安全通道