C#:System.Security.Authentication.AuthenticationException:根据验证程序,远程证书无效

时间:2021-06-30 11:49:45

标签: c# api exception httprequest httpresponse

我使用此方法向 API 发出 POST 请求:

 public async Task Login2Async()
    {
        
        var body = new 
        {
            username = "string",
            password = "string",
            clientName = "string",
            authorizationToken = "string",
            siteId = "string",
            siteName = "string",
            clientId = "string",
            clientVersion = "string",
            accessToken = "string"
        };

        var json = JsonSerializer.Serialize(body);

        var content = new StringContent(json, Encoding.UTF8, "application/json");
        var httpClient = new HttpClient();
        var result = await httpClient.PostAsync("https://api_ip/endpoint", content);
        WriteLine(result.StatusCode);
    }

然后我在 Main 方法中调用它:

static async Task Main(string[] args)
    {
        var webhook = new AccWebEndpointConnector("localhost", 8000);
        await webhook.Login2Async();

    }

但是我得到了这个堆栈跟踪,我不明白为什么。

    Unhandled exception. 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: RemoteCertificateNameMismatch, RemoteCertificateChainErrors
   at System.Net.Security.SslStream.SendAuthResetSignal(ProtocolToken message, ExceptionDispatchInfo exception)
   at System.Net.Security.SslStream.ForceAuthenticationAsync[TIOAdapter](TIOAdapter adapter, Boolean receiveFirst, Byte[] reAuthenticationData, Boolean isApm)
   at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Boolean async, Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)
   at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Boolean async, Stream stream, SslClientAuthenticationOptions sslOptions, 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.GetHttpConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(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.SendAsyncCore(HttpRequestMessage request, HttpCompletionOption completionOption, Boolean async, Boolean emitTelemetryStartStop, CancellationToken cancellationToken)
   at ACCWebEndpointConnector.AccWebEndpointConnector.Login2Async() in C:\Users\dfrco\TechWin\ACCWebEndpointConnector\AccWebEndpointConnector.cs:line 111
   at ACCWebEndpointConnector.Program.Main(String[] args) in C:\Users\dfrco\TechWin\ACCWebEndpointConnector\Program.cs:line 12
   at ACCWebEndpointConnector.Program.<Main>(String[] args)

我尝试使用 Postman 发出完全相同的 POST 请求,并且效果很好。

0 个答案:

没有答案
相关问题