title: The SSL connection could not be established, see inner exception. Win32Exception: The message received was unexpected or badly formatted
body: <p>I need to download a document from url but I'm getting SSL connection error,
  I have tried all the solutions shared on stack overflow, which were almost the same but I'm still getting this error,
  AuthenticationException: Authentication failed because the remote party sent a TLS alert: 'HandshakeFailure'.
  Win32Exception: The message received was unexpected or badly formatted.</p>
  <pre><code>public async Task&lt;ClientResult&lt;HttpResponseMessage&gt;&gt; GetDocumentData(string url)
              {
                  var getDocumentDataResult = new ClientResult&lt;HttpResponseMessage&gt;();
  
                  ServicePointManager.ServerCertificateValidationCallback = delegate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
                  try
                  {
                      using (var httpClientHandler = new HttpClientHandler())
                      {
  
                      httpClientHandler.ServerCertificateCustomValidationCallback += (message, cert, chain, errors) =&gt; { return true; };
  
                          //Setting TLS 1.2 protocol
                          httpClientHandler.SslProtocols = System.Security.Authentication.SslProtocols.Tls12;
  
                      var certFile = Path.Combine(&quot;C:\\Users\\myUser\\Documents&quot;, &quot;31075939.cer&quot;);
                      httpClientHandler.ClientCertificates.Add(new X509Certificate2(certFile));
                      certFile = Path.Combine(&quot;C:\\Users\\myUser\\Documents&quot;, &quot;30955730.cer&quot;);
                      httpClientHandler.ClientCertificates.Add(new X509Certificate2(certFile));
  
  
                          HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
                          result.Content.Headers.ContentType = new MediaTypeHeaderValue(&quot;application/pdf&quot;);
  
                          //var stream = new FileStream(url, FileMode.Open, FileAccess.Read);
                          Uri absoluteUri = new Uri(url, UriKind.Absolute);
  
                          HttpClient client = new HttpClient(httpClientHandler);
                          HttpResponseMessage response = await client.GetAsync(absoluteUri);
  
                          Byte[] bytes = await response.Content.ReadAsByteArrayAsync();
                          //result.Content = new StreamContent(stream);
  
                          getDocumentDataResult.StatusCode = response.StatusCode;
                          getDocumentDataResult.ResultContent = bytes.ToString();
  
                      }
              }
                  catch (Exception ex)
                  {
                      getDocumentDataResult.ExceptionText = ex.Message;
                      getDocumentDataResult.StatusCode = HttpStatusCode.InternalServerError;
                  }
                  return getDocumentDataResult;
              }
      }
  }
  
  </code></pre>
  <p>Complete Error is,</p>
  <p>System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.
  ---&gt; System.Security.Authentication.AuthenticationException: Authentication failed because the remote party sent a TLS alert: 'HandshakeFailure'.
  ---&gt; System.ComponentModel.Win32Exception (0x80090326): The message received was unexpected or badly formatted.
  --- End of inner exception stack trace ---
  at System.Net.Security.SslStream.ForceAuthenticationAsync[TIOAdapter](TIOAdapter adapter, Boolean receiveFirst, Byte[] reAuthenticationData, Boolean isApm)
  at System.Net.Http.ConnectHelper.EstablishSslConnectionAsync(SslClientAuthenticationOptions sslOptions, HttpRequestMessage request, Boolean async, Stream stream, CancellationToken cancellationToken)
  --- End of inner exception stack trace ---
  at System.Net.Http.ConnectHelper.EstablishSslConnectionAsync(SslClientAuthenticationOptions sslOptions, HttpRequestMessage request, Boolean async, Stream stream, CancellationToken cancellationToken)
  at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
  at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
  at System.Net.Http.HttpConnectionPool.AddHttp11ConnectionAsync(HttpRequestMessage request)
  at System.Threading.Tasks.TaskCompletionSourceWithCancellation`1.WaitWithCancellationAsync(CancellationToken cancellationToken)
  at System.Net.Http.HttpConnectionPool.GetHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
  at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
  at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
  at System.Net.Http.HttpClient.g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)</p>