'Not getting any responses from my http requests (System.Net.Http.HttpRequestException: 'Request aborted')

I am trying to consume my REST API using Xamarin with a POST request. However; I never get any responses to my web requests. The app just keeps awaiting them for minutes before finally throwing a System.Net.Http.HttpRequestException: 'Request aborted' exception. I have tried the same exact code in a .NET console application, where it works fine:

using Newtonsoft.Json;
using RestSharp;

Console.Write("Email: ");
string email = Console.ReadLine();
Console.Write("Password: ");
string password = Console.ReadLine();

string jwcUrl = "[URL]";
RestClient client = new RestClient(jwcUrl);

object data = new
{
    email = email,
    password = password
};

var myContent = JsonConvert.SerializeObject(data);
var request = new RestRequest().AddStringBody(myContent, DataFormat.Json);
var response = await client.PostAsync(request);

Console.WriteLine(response.Content);

The call gets through and eventually, I get my response. Not in the Xamarin app though. The function gets called from the OnClick over here:

public async void OnClick(View v)
        {
            var emailInput = FindViewById<EditText>(Resource.Id.inputEmail);
            var passwordInput = FindViewById<EditText>(Resource.Id.inputPassword);
            var responseTextField = FindViewById<TextView>(Resource.Id.twResponse);

            UserController userController = new UserController();

            string response = await userController.LogIn(emailInput.Text, passwordInput.Text);
            Toast.MakeText(Application.Context, response, ToastLength.Long).Show();
            responseTextField.Text = response;
        }

The code of which is here:

using RollCallAndroid.Models;
using RestSharp;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace RollCallAndroid.Controllers
{
    public class UserController
    {
        private const string jwcUrl = "[URL]";

        public async Task<string> LogIn(string email, string password)
        {
            User user = new User();
            RestClient client = new RestClient(jwcUrl);
            
            object data = new
            {
                email = email,
                password = password
            };

            var myContent = JsonConvert.SerializeObject(data);
            var request = new RestRequest().AddStringBody(myContent, DataFormat.Json);
            var response = await client.GetAsync(request);
            return response.StatusCode.ToString();
        }
    }
}

The issue isn't with RestSharp, the same thing happens when using the normal HttpClient. Even when trying a simple GetAsync on the default WeatherForecast, I get no response, no status code, nothing. Just silence until Visual Studio tells me the request was aborted.

Does anyone have any idea what's wrong? I'm out of ideas since the same request works fine everywhere else (Swagger, Postman, Console app). Thanks.



Solution 1:[1]

Usually for android when you have this kind of issue changing the SSL/TLS implementation to Native TLS 1.2+ helps, look at this answer

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Almis