0

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.

  • 2
    The cause is given: `---\> System.Security.Authentication.AuthenticationException: The remote certificate is invalid because of errors in the certificate chain: UntrustedRoot` – aybe Aug 03 '23 at 09:33
  • @aybe Thank you for your reply. But this code is working on .net3.1 and not working on .net6.0 And I tried HTTP request, but not HTTPS. Why should I get a certificate error? – Joseph Smith Aug 03 '23 at 09:45
  • Try this maybe: https://stackoverflow.com/questions/73403893/authenticationexception-the-remote-certificate-is-invalid-because-of-errors-in – aybe Aug 03 '23 at 09:49
  • 1
    @Josep Smith Do you get redirected to a HTTPS address? Your initial call might get a redirect response and automatically attempt to call the redirect URI. I'd suggest tracking the calls with a program like fiddler or wireshark to see what requests actually get sent. – Shazi Aug 03 '23 at 10:02
  • @Shazi Thank you for your reply. There is not redirect call. But HTTP URL contains https URL as the parameter. So I removed it and tested it. The result is okay. But I have to contain https URL as the parameter. I wonder why this happened. – Joseph Smith Aug 04 '23 at 05:50

1 Answers1

0

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()}");