I'm trying to send user information to a server using the post method. When I call an async Task, it immediatly crashes.
Here's what I have in LoginPage.cs
async void SignInProcedure(object sender, EventArgs e) //function called when pressing the signin button
{
User user = new User(Entry_Username.Text, Entry_Password.Text); //putting username and password entered by the user in the entry box
if (user.CheckInformation())//checks if the boxes are empty
{
try
{
var result = await App.RestService.Login(user);//RestService class
await App.Current.MainPage.DisplayAlert("Login", "Login Successful", "Oke");
if (result.access_token != null)
{
App.UserDatabase.SaveUser(user);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
And here in the RestService.cs
public async Task<Token> Login(User user)//this is where the problem is
{
var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("grant_type", grant_type));
postData.Add(new KeyValuePair<string, string>("username", user.Username));
postData.Add(new KeyValuePair<string, string>("password", user.Password));
var content = new FormUrlEncodedContent(postData);
var response = await PostResponseLogin<Token>(Constants.LoginUrl, content);
DateTime dt = new DateTime();
dt = DateTime.Today;
response.expire_date = dt.AddSeconds(response.expire_in);
return response;
}
public async Task<T> PostResponseLogin<T>(string weburl, FormUrlEncodedContent content) where T : class
{
var response = await client.PostAsync(weburl,content);
var jsonResult = response.Content.ReadAsStringAsync().Result;
var responseObject = JsonConvert.DeserializeObject<T>(jsonResult);
return responseObject;
}
The server should receive the user data, but it's not receiving anything.
The app crashes before putting the exception in the debug console.