2017-04-06 261 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/");