Httpclient Xamarin Forms call not working in Android

Viewed 1969

I am having a strange problem that occurs when debugging the xamarin forms app in android physical device.

The app goes to break mode and here is the output i receive:

  Mono.Android[0xd6be8960] -> System[0xd6be9680]: 14
    11-24 23:44:44.098 I/Choreographer(18685): Skipped 930 frames!  The application may be doing too much work on its main thread.
    Thread started: <Thread Pool> #8
    Thread started: <Thread Pool> #9
    11-24 23:44:44.807 D/Mono    (18685): Assembly Ref addref Mono.Security[0xc17989e0] -> System[0xd6be9680]: 15
    Thread started: <Thread Pool> #10
    An unhandled exception occured.

I tried many solutions such as allowing the network access in the app, made sure that the all needed packages are there such as:

Microsoft.Bcl.Build
Microsoft.net.HTTP
Newtonsoft.json

This is my code which works fine in UWP var request = new HttpRequestMessage();

    request.RequestUri = new Uri("https://jsonplaceholder.typicode.com/users");
    request.Method = HttpMethod.Get;

    request.Headers.Add("Accept", "application/json");
    var client = new HttpClient();
    HttpResponseMessage response = await client.SendAsync(request);
    HttpContent content = response.Content;
    var statusCode = response.StatusCode;
    var json = await content.ReadAsStringAsync();

EDIT: I am getting 2 main errors in the device log:

[ERROR] FATAL UNHANDED EXCEPTION: System.Net.HttpRequestException:An error occurred while sending the request --> System.Net.WebException:Error:SecureChannelFailure(The authentication or decryption has failed)
2 Answers

Change the default SSL/TLS implementation in Xamarin.Android.

Go to Android Project settings->Android Options->Advanced->SSL/TLS implementation and set it to Native TLS 1.2+

enter image description here

On some Cases you need to chagne the HttpClient Implementation to be AndroidClientHandler instead of Default as the following:

enter image description here

Related