.net 核心 API Post 异常给出 NativeErrorCode 12175 [英] .net core API Post exception gives NativeErrorCode 12175

查看:55
本文介绍了.net 核心 API Post 异常给出 NativeErrorCode 12175的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

for the code below, it catches an HttpRequestException/ System.Net.Http.WinHttpException when called

异常状态:

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() 

但是在同一个端点、资源、标题和正文上使用 Postman,我得到了 200.

but using Postman on the same endpoint, resource, headers and body, I get a 200 back.

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>

<小时>

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

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.

损坏的代码:

// GET: api/Accounts
[HttpGet]
public async Task<IActionResult> Get()
{
    try
    {
        using (var client = new HttpClient())
        {
            try
            {
                client.BaseAddress = new Uri("https://test-org.com/");
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "account");
                request.Content = new StringContent("<?xml version="1.0" encoding="UTF-8"?>
<accountMessage xml...</accountMessage>",Encoding.UTF8,"application/xml");

                request.Headers.Add("X-HTTP-Method-Override", "GET");
                var response = await client.SendAsync(request);

                response.EnsureSuccessStatusCode(); 
                var stringResponse = await response.Content.ReadAsStringAsync();
                var posts = JsonConvert.DeserializeObject<IEnumerable<Post>>(stringResponse);

                if (posts == null) return NotFound($"Posts were not found");
                return Ok(posts);
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine($"Request exception: {e.Message}");
            }
        }
    }
    catch (Exception)
    {

    }
    return BadRequest();
}

推荐答案

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

I talked with the owner of the server/service. They are providing a brand new service, and the service is currently using a self-signed certificate. Until the server/service is using a real certificate that can be verified via a certificate authority, Im going to bypass certificate verification in my code:

            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/");

这篇关于.net 核心 API Post 异常给出 NativeErrorCode 12175的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆