C# Core 3.1 - Getting error message "The SSL connection could not be established" when calling HttpClient SendAsync()
Asked Answered
L

0

4

Env: Console App, C# Core 3.1, VS 2019

I'm executing the following line:

var response = await client.SendAsync(request).ConfigureAwait(false);

I'm getting the following error:

{"The SSL connection could not be established, see inner exception."}
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2146233087
    HelpLink: null
    InnerException: {"Authentication failed, see inner exception."}
    Message: "The SSL connection could not be established, see inner exception."
    Source: "System.Net.Http"
    StackTrace: "   at System.Net.Http.ConnectHelper.<EstablishSslConnectionAsyncCore>d__4.MoveNext()\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at ...

Did some googling and found this:

https://github.com/dotnet/runtime/issues/28189

So I added

AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false);

right before the above line. Still getting the error.

I have a bunch of code prior to the executed line:

httpClientHandler = new HttpClientHandler
{
    SslProtocols = SslProtocols.Tls12
};

Plus code to add the certificate, header, content-type, etc. Not sure what is actually causing the error. Any ideas?

Thanks,

Lictor answered 24/2, 2020 at 15:42 Comment(6)
Added the following:httpClientHandler.ServerCertificateCustomValidationCallback = (sender,certificate,chain,sslPolicyError)=>{return true;}; . This gives me a new error: System.Net.Http.HttpRequestException: An error occurred while sending the request. --->System.Net.Http.WinHttpException(80072F8F,12175):Error12175 calling WINHTTP_CALLBACK_STATUS_REQUEST_ERROR, 'A security error occurred'. at System.Threading.Tasks.RendezvousAwaitable`1.GetResult() at System.Net.Http.WinHttpHandler.StartRequestAsync(WinHttpRequestState state) --- End of inner exception stack trace ---Lictor
i got this same error in dotnet core 2.2Crinum
I ended up using the RestSharp library.Lictor
Can you share your solution? I also tried to switch to RestSharp because of that error buit I keep getting it with RestSharpIntrepid
Unfortunately I don't remember what I did to resolve this.Lictor
@Lictor do you happen to recall now what you did to resolve this?Obtrusive

© 2022 - 2024 — McMap. All rights reserved.