.net core API Post异常给出了NativeErrorCode 12175

use*_*225 4 c# dotnet-httpclient .net-core

对于下面的代码,它在调用时捕获HttpRequestException/System.Net.Http.WinHttpException

异常状态:NativeErrorCode 12175
消息"发生安全性错误"

  • e {System.Net.Http.HttpRequestException:发送请求时发生错误.---> System.Net.Http.WinHttpException:System.Runtime.CompilerServices.ConfiguredTaskAwaitable上System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()发生安全性错误.__CODE__1.ConfiguredTaskAwaiter.GetResult()at System.Net.Http.HttpClient.d__58.MoveNext()---从抛出异常的先前位置开始的堆栈跟踪结束---在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()在Controllers.Controller.d__0.MoveNext()的System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()上的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)

但是在相同的端点,资源,标题和正文上使用Postman,我得到200回.

NativeErrorCode 12175   
Message "A security error occurred"

-       e   {System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.WinHttpException: A security error occurred
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Net.Http.WinHttpHandler.<StartRequest>d__105.MoveNext()
   --- End of inner exception stack trace ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Net.Http.HttpClient.<FinishSendAsync>d__58.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Controllers.Controller.<Get>d__0.MoveNext() 
Run Code Online (Sandbox Code Playgroud)

当我搜索NativeErrorCode 12175 .NET时,我发现这个:https://msdn.microsoft.com/en-us/library/windows/desktop/aa383770(v = vs.85).aspx

POST /account/ HTTP/1.1
Host: test-org.com
X-HTTP-Method-Override: GET
Content-Type: application/xml
Cache-Control: no-cache
Postman-Token: ce565d1a-bfb7-0961-ffd7-d279b90e97c5

<?xml version="1.0" encoding="UTF-8"?>
<accountMessage xmlns="http://sdfssd
........
</accountMessage>
Run Code Online (Sandbox Code Playgroud)

破坏的代码:

ERROR_WINHTTP_SECURE_FAILURE
12175
One or more errors were found in the Secure Sockets Layer (SSL) certificate sent by the server. To determine what type of error was encountered, check for a WINHTTP_CALLBACK_STATUS_SECURE_FAILURE notification in a status callback function. For more information, see WINHTTP_STATUS_CALLBACK.
Run Code Online (Sandbox Code Playgroud)

use*_*225 13

我和服务器/服务的所有者交谈过.他们提供全新的服务,该服务目前正在使用自签名证书.在服务器/服务使用可以通过证书颁发机构验证的真实证书之前,我将在我的代码中绕过证书验证:

            using (var httpClientHandler = new HttpClientHandler())
            {

                httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
                using (var client = new HttpClient(httpClientHandler))
                {
                    try
                    {

                        client.BaseAddress = new Uri("https://test-org.com/");
Run Code Online (Sandbox Code Playgroud)