locked
using HttpClient RRS feed

  • Question

  • User-2070496060 posted

    I have a few lines of code I was using in a console application using HttpClient and now wanting to use them inside ASP.NET seems like the request gets sent ok but unable to wait for the response. Just hangs there forever;

    var resTask = client.PostAsync(APIUrl, content);
    var response = await resTask;

    First line executes ok, second one just hangs forever. The request is received on the server.  Using the recommended one static HttpClient

    static readonly HttpClient client = new HttpClient();

    Thursday, January 2, 2020 10:57 AM

Answers

  • User-2070496060 posted

    Turned out there were a few problems here. I was calling the code originally from Page_Load which it did not like at all. So I moved that to a button handler and i also had to add Async="true" in the @page decoration at the top of the page. Once that was done it runs fine.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 5, 2020 1:05 AM

All replies

  • User753101303 posted

    Hi,

    Not even a timeout if waiting long enough? I'm not sure I ever saw really something being just "hanged".

    As the request made it to the other server, it seems that for some reason the response is blocked on its way back to the source (maybe a firewall issue ?)

    Thursday, January 2, 2020 12:07 PM
  • User765422875 posted

    Did this work in the console application? The code seems a bit incomplete. It should resemble something like this:

     var response = await client.PostAsync(APIUrl, content); // content is of type StringContent
     
     response.EnsureSuccessStatusCode();
     var result = await response.Content.ReadAsStringAsync();
     
     return result; // result can be serialized into an object

    Refer the following as well: https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client

    Thursday, January 2, 2020 7:21 PM
  • User-2070496060 posted

    Yea that is exactly what I am doing minus your EnsureSuccessStatusCode.

    I was playing with the format to try different things to get it to work here is the original format that works in the console but not in ASP.NET application.

    //execute
    var response = await client.PostAsync(APIUrl, content);

    Log("Got Response: " + response.StatusCode.ToString());

    //wait for the response content
    var responseTask = response.Content.ReadAsStringAsync();
    var responseString = await responseTask;

    I am also running this locally still on IIS Express. There is no timeout just hangs forever. (or as long as I have let it maybe 5 minutes so far).

    Thursday, January 2, 2020 11:00 PM
  • User288213138 posted

    Hi Martin,

    //execute
    var response = await client.PostAsync(APIUrl, content);

    Log("Got Response: " + response.StatusCode.ToString());

    //wait for the response content
    var responseTask = response.Content.ReadAsStringAsync();
    var responseString = await responseTask;

    According to your description, I can't reproduce your question.

    But here is a similar question you can use as a reference:https://stackoverflow.com/questions/34078296/httpclient-postasync-never-return-response

    It may a deadlock in your code. The reason this is working in a console application is because console applications don't have contexts, but ASP.NET apps do (see http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html). You should make the Signin method in your controller async and await the call to _authenticationService.Authenticate to resolve the deadlock issue.

    Hope this can help you. If you can't solve the problem, please post your code behind.

    Best regards,

    Sam

    Friday, January 3, 2020 7:59 AM
  • User765422875 posted

    Is the HttpClient code within an async method? I assume it is because you are using the HttpClient's async methods.

    Friday, January 3, 2020 12:58 PM
  • User-2070496060 posted

    Turned out there were a few problems here. I was calling the code originally from Page_Load which it did not like at all. So I moved that to a button handler and i also had to add Async="true" in the @page decoration at the top of the page. Once that was done it runs fine.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 5, 2020 1:05 AM