[WPF] HttpClientHandler SendAsync Call Never Getting HTTP Response Back

Magi 21 Reputation points
2022-03-11T05:55:35.95+00:00

Our project is developed with WPF C# with Target framework version of .Net Framework 4.6.2.

We were trying to do a POST call using HttpClientHandler, But we didn't get any response or the status back.
The application stays hanged, Didn't receiving any response. I am attaching my codes with sample URL for your reference. Help me with this issue.

182123-1.png

The code hangs in the line
var result = await HttpCl.SendAsync(request);

We also didn't get any exception or error in the console.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,783 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,995 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
814 questions
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,326 Reputation points
    2022-03-12T19:58:54.497+00:00

    Hi Magi,
    i use this code without problems:

    private async Task<AsyncObservableCollection<ContinentModel>> GetContinentModels()
    {
      AsyncObservableCollection<ContinentModel> ReturnContinentModels = new AsyncObservableCollection<ContinentModel>();
      try
      {
        using (HttpClient httpClient = new HttpClient())
        {
          httpClient.DefaultRequestHeaders.Add("Accept", "application/json;charset=UTF-8");
    
          using (HttpResponseMessage HttpResponseMessage = await httpClient.GetAsync(string.Concat(_RootUrl, ""), HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false))
          {
            if (HttpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK)
            {
              using (HttpContent HttpContent = HttpResponseMessage.Content)
              {
                string MyContent = await HttpContent.ReadAsStringAsync();
                var root = JsonConvert.DeserializeObject<RootObject>(MyContent);
                ReturnContinentModels = new AsyncObservableCollection<ContinentModel>(root.products);
              }
            }
          }
        }
      }
      catch (Exception ex)
      {
        Console.WriteLine(ex.Message);
      }
      return ReturnContinentModels;
    }
    

1 additional answer

Sort by: Most helpful
  1. Gerd Canori 1 Reputation point
    2022-03-11T10:06:54.39+00:00

    var res = Task.Run() => GetVersionTest().Result;

    private static async Task GetVersionTest()
    {

    var result = .....

    if(result.StatusCode == HttpStatusCode.OK
    {
    var resp = result.Content.ReadAsStringAsync();
    .....

    }

    }

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.