2017-04-06 260 views
0

下面的代码,它捕捉到一个HttpRequestException/System.Net.Http.WinHttpException时调用.NET核心API邮政例外给人NativeErrorCode 12175

的异常状态: NativeErrorCode 12175
消息“发生了安全错误“

  • e {System.Net.Http.HttpRequestException:发送请求时发生错误。 ---> System.Net.Http.WinHttpException:System.Runtime发生安全错误 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 。 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.d__58.MoveNext() ---从以前位置抛出异常的堆栈跟踪结束--- 在System.Runtime。在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 在Controlle上System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)上的ExceptionServices.ExceptionDispatchInfo.Throw() rs.Controller.d__0.MoveNext()

但在相同的端点,资源,标题和正文上使用邮差,我得到了200回。

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\"?>\r\n<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(); 
    } 
+0

你检查'WINHTTP_CALLBACK_STATUS_SECURE_FAILURE'?你使用什么样的证书? – VMAtm

回答

6

我与交谈服务器/服务的所有者。他们正在提供全新的服务,该服务目前正在使用自签名证书。直到服务器/服务的使用可以通过证书颁发机构进行验证一个真正的证书,我就要绕过我的代码证书验证:

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