I have some .net code for test.


 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:5000/convert?inputURL=https://storage.googleapis.com/...");

 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
 if (response.StatusCode != HttpStatusCode.OK)
 {
     return false;
 }
 return true;

I used this code without error in .net3.1 But I'm getting the following error in .net6.0


System.Net.WebException: The SSL connection could not be established, see inner exception. ---\> System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception. ---\> System.Security.Authentication.AuthenticationException: The remote certificate is invalid because of errors in the certificate chain: UntrustedRoot
Stack Trace:
at System.Net.Security.SslStream.SendAuthResetSignal(ProtocolToken message, ExceptionDispatchInfo exception)
at System.Net.Security.SslStream.CompleteHandshake(SslAuthenticationOptions sslAuthenticationOptions)
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.WaitWithCancellation(CancellationToken cancellationToken)    at System.Threading.Tasks.TaskCompletionSourceWithCancellation`1.WaitWithCancellationAsync(Boolean async, 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.HttpMessageHandlerStage.Send(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
at System.Net.Http.HttpMessageHandlerStage.Send(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.SocketsHttpHandler.Send(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpMessageInvoker.Send(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.Send(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
at System.Net.HttpWebRequest.SendRequest(Boolean async)
at System.Net.HttpWebRequest.GetResponse()
\--- End of inner exception stack trace ---

Even though I'm trying to get an HTTP response, the .net exception says "The SSL connection could not be established"

.net use SSL connection for getting HTTP response?

How can I resolve this exception?

I tried The SSL connection could not be established

But getting the equal exception.

1

There are 1 best solutions below

2
Joseph Smith On

I found the reason in Shazi's comment.

If the HTTP URL contains the HTTPS URL with the parameter, this exception happens.

Now that I know the cause, there are many ways to solve it. In my case, I encoded the HTTPS URL parameter.

Prev:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:5000/convert?inputURL=https://storage.googleapis.com/...");

Now:

 NameValueCollection queryString = HttpUtility.ParseQueryString(string.Empty);
 queryString.Add("inputUrl", "https://storage.googleapis.com/...");
 HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"http://localhost:5000/convert?{queryString.ToString()}");