C# stops working when performing async post request

  • 0
  • Pump00:00

I am working on a mobile app, and the problem is that when I perform a async request ( PostAsync ) using Net.Http my program stops running.

Here is my request class, where I perform the requests using Net.Http.

...

namespace BSoft.Requests
{
   public class Requests
    {
      public Requests(){}

       public static string HostName =  "https://dev5.360businesssoft.com/";

    private static readonly HttpClient httpClient = new HttpClient();

    public static async Task<string> PerformPostRequest(Dictionary<string, string> values, string path)
    {
        string url = HostName + path;
        FormUrlEncodedContent content = new FormUrlEncodedContent(values);
        HttpResponseMessage response = await httpClient.PostAsync(url, content);
        string responseString = await response.Content.ReadAsStringAsync();
        return responseString;
    }

}
}

and here is my login class, where I call call the request and display the result as string.

... 

namespace BSoft.Login
{
public class Login
{
    public Login()
    {
    }      

    public static void PerformLogin(string username, string password, bool remember)
    {
        var values = new Dictionary<string, string>();
        values.Add("User", username);
        values.Add("Password", password);

        var ReturnedObj = Requests.Requests.PerformPostRequest(values, "test.php").Result;
        System.Diagnostics.Debug.WriteLine(ReturnedObj);
    }
}
}

This is a screenshot of the app, you can notice that the button is freezed

Default Asked on December 28, 2019 in Programming.
Add Comment
  • 0 Answer(s)
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.