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

时间:2013-10-25 13:32:03

标签: c# ssl httpwebrequest httpwebresponse dotnet-httpclient

服务器团队说这个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 个答案:

答案 0 :(得分:1)

尝试把这个

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

之前