Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Install Newtonsoft.Json NuGet package and put "using Newtonsoft.Json;" in the beginning of the file
- public static async Task<bool> HttpRequest(string email, string password)
- {
- string loginJson = JsonConvert.SerializeObject(new { Email = email, Password = password});
- StringContent content = new StringContent(loginJson, Encoding.UTF8, "application/json");
- HttpResponseMessage result = await client.PostAsync("http://localhost:64854/api/logins", content); // check the port number after starting the API server
- if (result.IsSuccessStatusCode)
- {
- string response = await result.Content.ReadAsStringAsync();
- Debug.WriteLine(response);
- // If you copy the Login model from the API project, you can parse the response into an object:
- // Login loginDetails = JsonConvert.DeserializeObject<Login>(response);
- return true;
- }
- else
- {
- return false;
- }
- }
- public async void LoginMethod()
- {
- try
- {
- bool isSuccess = Task.Run(async () => await HttpRequest(Email, Password)).Result;
- Debug.WriteLine($"Is login succeeded: {isSuccess}");
- }
- catch (AggregateException ex)
- {
- Debug.WriteLine(ex);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement