简体   繁体   English

c# 抛出异常:System.dll 中的“System.Net.WebException”

[英]c# Exception thrown: 'System.Net.WebException' in System.dll

I am trying to send a simple HttpWebRequest, where I am trying to logout the current logged in user, The same problem also exists when trying to login the user and I get exception erro "'System.Net.WebException' in System.dll"我正在尝试发送一个简单的 HttpWebRequest,我试图在其中注销当前登录的用户,尝试登录用户时也存在同样的问题,并且出现异常错误“System.Net.WebException” in System.dll”

So the HttpWebRequest, that I am trying to send is as follows:所以我试图发送的 HttpWebRequest 如下:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://localhost:61000/users/logout");
request.Method = "POST";
try
{
    HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse();
}
catch (Exception ex)
{
    Debug.WriteLine("Exception Occurred: " ex.Message);
}

I am working with the visual studio 2017 and there I try to look for more information on the below exception: Exception thrown: 'System.Net.WebException' in System.dll我正在使用 Visual Studio 2017,在那里我尝试查找有关以下异常的更多信息: 异常抛出:System.Net.WebException 中的 System.dll

-       $exception  {System.Net.WebException: The SSL connection could not be established, see inner exception. The remote certificate is invalid according to the validation procedure. ---> System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception. ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.
   at System.Net.Security.SslState.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, ExceptionDispatchInfo exception)
   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.PartialFrameCallback(AsyncProtocolRequest asyncRequest)
--- End of stack trace from previous location where exception was thrown ---
   at System.Net.Security.SslState.ThrowIfExceptional()
   at System.Net.Security.SslState.InternalEndProcessAuthentication(LazyAsyncResult lazyResult)
   at System.Net.Security.SslState.EndProcessAuthentication(IAsyncResult result)
   at System.Net.Security.SslStream.EndAuthenticateAsClient(IAsyncResult asyncResult)
   at System.Net.Security.SslStream.<>c.<AuthenticateAsClientAsync>b__47_1(IAsyncResult iar)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
   at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   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 System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
   at System.Net.HttpWebRequest.SendRequest()
   at System.Net.HttpWebRequest.GetResponse()
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.GetResponse()
   at ZenZefi.Program.Main(String[] args) in H:\work\Zenzefi\ZenZefi\ZenZefi\Program.cs:line 25}    System.Net.WebException

Since the exception message is about SSL, i think that the error thrown because invalid or expired SSL certificate.由于异常消息是关于 SSL 的,我认为是因为 SSL 证书无效或过期而引发的错误。

You can try to ignore them by specifying ValidationCallback before creating a request.您可以通过创建请求之前指定ValidationCallback来尝试忽略它们。

ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

Thanks to Alex K for reminding me.感谢Alex K提醒我。 Accepting invalid, expired or untrusted SSL certificates is really bad workaround.接受无效、过期或不受信任的 SSL 证书是非常糟糕的解决方法。 It can absolutely causing serious problem, makes your server become vulnerable.它绝对会导致严重的问题,使您的服务器变得脆弱。 So the best practice here is to ensure that the SSL certificate is valid.所以这里的最佳实践是确保 SSL 证书有效。

如果您没有创建有效的证书,则会出现安全异常,请在此处找到解决方案如何在 .NET 中验证 HTTPS/SSL 证书的有效性?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM