0

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.

2 Answers 2

1

Try to put this

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

before everything

Sign up to request clarification or add additional context in comments.

1 Comment

I had not been receiving the above mentioned lately. It would be nice if you could let me know the purpose of this code. Thank you!
0

I was the same problem. put the below code before WebRequest.Create

 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3; ServicePointManager.ServerCertificateValidationCallback = (wsender, certificate, chain, erros) => { return true; }; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.