Home C# stops working when performing async post request
Reply: 0

C# stops working when performing async post request

user1624
1#
user1624 Published in June 19, 2018, 2:25 pm

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

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.71504 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO