简体   繁体   中英

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

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.Task 1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task 1.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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM