简体   繁体   English

如何确定HttpClient.PostAsJsonAsync调用上500错误的问题是什么?

[英]How do I determine what is the issue with a 500 error on a HttpClient.PostAsJsonAsync call?

I am making a call to a Web API that I wrote. 我正在调用我编写的Web API。 I am working through the bugs on both sides and am getting a 500 error. 我正在解决双方的错误,并收到500错误。 I want to see that is in that error message to see what might be the problem. 我想看到那个错误消息,看看可能是什么问题。 How do I find that? 我怎么找到的?

using (var client = new HttpClient())
{
    var fooURL = Url.RouteUrl("PayrollApi", new { httproute = string.Empty, controller = "LeaveRequest" }, Request.Url.Scheme);
    var repsonse = client.PostAsJsonAsync(fooURL, leaveRequest).Result;
}

I don't see where that text might be stored so I can figure out what other bugs need to be fixed. 我没有看到该文本可能存储在哪里,因此我可以找出需要修复的其他错误。 How do I get that text? 我如何获得该文本?

Update : something I didn't clarify. 更新 :我没有澄清的事情。 I put a breakpoint on the WebAPI I am calling and the break point is never hit. 我在我调用的WebAPI上放了一个断点,断点永远不会被击中。 However , when I call it from Poster, I hit the break point. 然而 ,当我从海报中调用它时,我达到了断点。 The URL is correct. 网址是正确的。

public HttpResponseMessage Post([FromBody]LeaveRequest value)
{
    if (ModelState.IsValid)
    {
        // code here
    }
}

I was able to make a change to get the error message: 我能够进行更改以获取错误消息:

var repsonse = client.PostAsJsonAsync(fooURL, leaveRequest).Result.Content.ReadAsStringAsync();

instead of 代替

var repsonse = client.PostAsJsonAsync(fooURL, leaveRequest).Result;

I was then able to see my error and fix it. 然后我能够看到我的错误并修复它。

I am making a call to a Web API that I wrote. 我正在调用我编写的Web API。 I want to see that is in that error message to see what might be the problem. 我想看到那个错误消息,看看可能是什么问题。

You might want to put the code you have in PayrollApi.LeaveRequest in a try-catch block, something like: 您可能希望将PayrollApi.LeaveRequest中的代码放在try-catch块中,例如:

public HttpResponseMessage LeaveRequest()
{
    try {
        // do something here
    } 
    catch(Exception ex) {
        // this try-catch block can be temporary 
        // just to catch that 500-error (or any unexpected error)
        // you would not normally have this code-block
        var badResponse = request.CreateResponse(HttpStatusCode.BadRequest);
        badResponse.ReasonPhrase = "include ex.StackTrace here just for debugging";
    }
}

Then make a call inside a try-catch block to capture that extra error: 然后在try-catch块内调用以捕获额外的错误:

using (var client = new HttpClient())
{
    // rest of your code goes here
    try
    {
        var repsonse = client.PostAsJsonAsync(fooURL, leaveRequest).Result;
    }
    catch(HttpRequestException httpEx)
    {
         // determine error here by inspecting httpEx.Message         
    }
}

httpEx.Message can have this message: httpEx.Message可以有这样的消息:

Response status code does not indicate success: 500 ({stack_trace_goes_here}). 响应状态代码不表示成功:500({stack_trace_goes_here})。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HttpClient.PostAsJsonAsync 无法从 ASPX 工作的问题 - Issue with HttpClient.PostAsJsonAsync Not Working from ASPX HttpClient.PostAsJsonAsync引发TaskCanceledException - HttpClient.PostAsJsonAsync throws TaskCanceledException C# HttpClient.PostAsJsonAsync 因内部服务器错误而失败 - C# HttpClient.PostAsJsonAsync Failing with Internal Server Error 如何从httpClient.PostAsJsonAsync绑定JSON响应 - How to bind JSON response from httpClient.PostAsJsonAsync HttpClient.PostAsJsonAsync崩溃而不抛出异常 - HttpClient.PostAsJsonAsync crashing without throwing exception HttpClient.PostAsJsonAsync与MVC控制器动作之间的通信 - Communication between HttpClient.PostAsJsonAsync and MVC controller actions 通过HTTPClient.PostAsJsonAsync调用的JsonConverter中的HttpContext不可用 - HttpContext unavailable in JsonConverter called via HTTPClient.PostAsJsonAsync HttpClient.PostAsJsonAsync永远不会看到帖子成功和响应的时间 - HttpClient.PostAsJsonAsync never sees when the post is succeeding and responding 使用HttpClient PostAsJsonAsync调用我的API,如何正确接受API中的复杂类型? - Using HttpClient PostAsJsonAsync to call my API, how do properly accept the complex type in the API? C#HttpClient.PostAsJsonAsync()失败,即使通过PostMan发出的请求是完全相同的请求 - C# HttpClient.PostAsJsonAsync() fails, even though the exact same request is accepted when made through PostMan
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM