Runtime: The SSL connection could not be established, see inner exception

Created on 13 Dec 2018  路  7Comments  路  Source: dotnet/runtime

System
OS : Windows Server 2012 R2 Standard
.Net Core : 2.1.5

Problem
HTTPS address request, sometimes this error occurs, not every request will be wrong

The SSL connection could not be established, see inner exception.

Details

The SSL connection could not be established, see inner exception.
   at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask`1 creationTask)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
   at Rookies.Pay.Infrastructure.HttpClientWrap.PostAsync(String url, String postData, IDictionary`2 headers, String contentType, Int32 timeout, Encoding encoding) in E:/...

Inner Exception:
Authentication failed, see inner exception.

Code:
```c#
public async Task PostAsync(string url, string postData, IDictionary headers = null, string contentType = "application/x-www-form-urlencoded", int timeout = 0, Encoding encoding = null)
{
HttpResp resp = new HttpResp();

        var client = _httpClientFactory.CreateClient();
        if (headers != null)
        {
            foreach (KeyValuePair<string, string> header in headers)
            {
                client.DefaultRequestHeaders.Add(header.Key, header.Value);
            }
        }
        if (timeout > 0)
        {
            client.Timeout = new TimeSpan(0, 0, timeout);
        }
        using (HttpContent content = new StringContent(postData ?? "", encoding ?? Encoding.UTF8))
        {
            if (contentType != null)
            {
                content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
            }
            using (HttpResponseMessage responseMessage = await client.PostAsync(url, content))
            {
                resp.StatusCode = responseMessage.StatusCode;
                byte[] resultBytes = await responseMessage.Content.ReadAsByteArrayAsync();
                resp.ResponseText = (encoding ?? Encoding.UTF8).GetString(resultBytes);
            }
        }

        return resp;
    }

```

area-System.Net.Http needs more info

All 7 comments

What websites/URL's does this error occur with?

@davidsh > What websites/URL's does this error occur with?
Sorry for waiting so long to reply to you.
This error occurred when the URL was requested, but not every time, only occasionally
https://gateway.suhui.org/api/v1/order

Real bug: "see inner exception". Don't do that. Instead do

SslException(string message, Exception inner) : base(string_ssl_could_not_be_established + " " + inner.Message, inner)

@Rwing @joshudson i'm with the same issue and when i try to follow your link, i start getting more SSL error, any idea?

@caiogranero In my case, just simply set DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER to false everything is done.

Triage: Sadly, this is not actionable without a repro at this point.
There may have been improvements in .NET Core 3.0, so it is worth to try that.
We are working on improving SSL connection diagnostics, incl. considering changes to the exception (not 100% clear we're going to do that though) - @wfurt is now driving that effort actively.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aggieben picture aggieben  路  3Comments

bencz picture bencz  路  3Comments

omajid picture omajid  路  3Comments

GitAntoinee picture GitAntoinee  路  3Comments

jchannon picture jchannon  路  3Comments