Get request error "Could not create SSL/TLS secure channel"

0

I try request site page from ConsoleApp:

static void Main(string[] args)
    {
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
        var handler = new HttpClientHandler
        {
            AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
            ,
            AllowAutoRedirect = false
        };

        using (var client = new HttpClient(handler))
        {
            var response = client.GetAsync("https://lk.fssprus.ru/ds_cabinet/action/login");
            var httpResponseMessage = response.Result;
            var content = httpResponseMessage.Content.ReadAsStringAsync();
            Console.WriteLine(content.Result);
        }
        Console.ReadKey();
    }

and get this error:

System.AggregateException HResult=0x80131500 Message=One or more errors occurred. Source=mscorlib StackTrace: at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task1.get_Result() at ConsoleApp8.Program.Main(String[] args) in C:\Projects\FsspConnectTest\ConsoleApp8\Program.cs:line 26

This exception was originally thrown at this call stack: System.Net.HttpWebRequest.EndGetResponse(System.IAsyncResult) System.Net.Http.HttpClientHandler.GetResponseCallback(System.IAsyncResult)

Inner Exception 1: HttpRequestException: An error occurred while sending the request.

Inner Exception 2: WebException: The request was aborted: Could not create SSL/TLS secure channel.

I can't resolve this problem

Update: I test on Windows 10 and it's work perfect, but on my Windows 8.1 not work. What need install for Windows 8.1?

c#
.net-core
frameworks
asked on Stack Overflow Mar 25, 2020 by Voucik • edited Mar 25, 2020 by Voucik

1 Answer

1

There are two things -

  1. "In .NET Core, ServicePointManager affects only HttpWebRequest. It does not affect HttpClient. In .NET Framework, the built-in HttpClient is built on top of HttpWebRequest, therefore ServicePointManager settings will apply to it."

    So set security protocol in HttpClientHandler that you're passing into HttpClient constructor.

  2. Verify if the required TLS version is disabled on the machine. Since you said it works just fine in one place but fails in another and TLS1.2 is the default protocol enabled on Windows 8.1 it could be the case. So here is how you do it - Transport Layer Security (TLS) registry settings.

answered on Stack Overflow Mar 26, 2020 by Kiryl Z • edited Jun 20, 2020 by Community

User contributions licensed under CC BY-SA 3.0