Intermittent error : The request was aborted: Could not create SSL/TLS secure channel

Viewed 982

We are using client written in C# .net 4.5 Console Application to consume a webservice (our webservice supports TLS and it doesn't supports SSL). 99 percentage of the time it works without any issues but sometimes we are getting the error message “The request was aborted: Could not create SSL/TLS secure channel” in a random manner. Within couple of milli seconds it will be back to normal as well. The OS of Client machine is Windows server 2012 r2. The same application is running without any issues from Windows 10.

static async Task<string> CheckStudentStatus(string studentID)
        {
            string studentstat = "error";
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            try
            {

                using (var httpClient = new HttpClient())
                {
                    using (var request = new HttpRequestMessage(new HttpMethod("GET"), "https://EndpointURL" + studentID))
                    {

                        request.Headers.TryAddWithoutValidation("Accept", "application/json");
                        request.Headers.TryAddWithoutValidation("Connection", "Keep-Alive");
                        request.Headers.TryAddWithoutValidation("Authorization", "Bearer AuthenticationToken");

                        var response = await httpClient.SendAsync(request);
                        var responseJson = System.Threading.Tasks.Task.Run(async () => await response.Content.ReadAsStringAsync())
                   .GetAwaiter().GetResult();
                        Console.WriteLine(responseJson.ToString());
                        if ((int)response.StatusCode == 200)
                        {
                            JObject joResponse = JObject.Parse(responseJson);
                            var studentStatus = joResponse.GetValue("StudentStatus");
                            string Stat = studentStatus?.ToString();
                            if (!string.IsNullOrWhiteSpace((Stat)))
                            {
                                studentstat = Stat;
                            }
                        }
                    }
                }
            }
            catch (HttpRequestException ex)
            {
                Console.WriteLine("Error message: " + ex.Message);
                Console.WriteLine("Stack Trace " + ex.StackTrace);
                Console.WriteLine("Inner exception " + ex.InnerException.Message);
            }
            return studentstat;
        }

0 Answers
Related