Question

The server team says that everything is fine with this HTTPS website, however the below code when using HTTPClient gives back "The request was aborted: Could not create SSL/TLS secure channel." error and at the same time works perfectly fine by returning back the intended data.

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;
}

In order to approach this issue in a different way I wrote another method which uses HTTPWebRequest and HTTPWebResponse. This code never gave me this error until now "The request was aborted: Could not create SSL/TLS secure channel."; However I wanted to post this out to find what would be the possible issue?

    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;
    }

Please do share your thoughts, however I doubt may be its the issue with using HTTPClient.

Was it helpful?

Solution

Try to put this

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

before everything

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top