Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
525 views
in Technique[技术] by (71.8m points)

rest - HTTP 500 response is received when using C# HttpClient

I have developed a C# application that calls a REST service existing in some PC in the network.

This is the code to make a request:

    public async Task<bool> OpenDoorAsync(string name, int delay)
    {
        var data = await CallApiAsync("api/door/remoteOpenByName", new Dictionary<string, string> { { "doorName", name }, { "interval", delay.ToString() } });
        return data.IsSuccess;
    }

    private async Task<ResponseData> CallApiAsync(string endPoint, Dictionary<string, string> parameters)
    {
        try
        {
            using (HttpClient client = new HttpClient())
            {
                client.Timeout = TimeSpan.FromSeconds(30);
                client.DefaultRequestHeaders.Connection.ParseAdd("keep-alive");

                var content = new StringContent(string.Empty, Encoding.UTF8, "application/json");
                string fullUri = "http://192.168.0.122:8088/api/door/remoteOpenByName?doorName=10.185.85.237-1&interval=5&access_token=1234";
                HttpResponseMessage response = await client.PostAsync(fullUri, content);
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();
                return JsonConvert.DeserializeObject<ResponseData>(responseBody);
            }
        }
        catch (Exception ex)
        {
            OnError("Existió un error al realizar la llamada.", ex);
            return new ResponseData()
            {
                message = "failed"
            };
        }
    }

Entry point is OpenDoorAsync, called this way, from a Winform form:

await _device.OpenDoorAsync(TxtNombrePuerta.Text.Trim(), IntInterval.Value);

Well, after the execution of PostAsync method, a HTTP 500 error is returned:

{StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Connection: close
  Date: Thu, 28 Jan 2021 21:06:35 GMT
  Set-Cookie: JSESSIONID=4062B932CDB44B4CA3FCCC275937AC15; Path=/; HttpOnly
  Server: Apache-Coyote/1.1
  Content-Length: 2580
  Content-Language: en
  Content-Type: text/html; charset=utf-8
}}

However, if I make the same request using Google Chrome RESTED extension, it works perfectly:

RESTED call

Just in case, I analyzed Google Chrome developer tools after the RESTED call and I have not found anything weird. I thought maybe I missed to send something else in the headers.

Developer tools

Does anybody know what is happening with the call from the C# application? Clearly, I am not doing something that RESTED is.

question from:https://stackoverflow.com/questions/65945912/http-500-response-is-received-when-using-c-sharp-httpclient

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...