7

I'm learning how to use xamarin with C#, and I've got an android app to make web requests to an external API and I'm receiving the following error;

System.Net.Http.HttpRequestException: An error occurred while sending the 
request ---> System.Net.WebException: Error: SecureChannelFailure (The   
authentication or decryption has failed.) ---> System.IO.IOException: The 
authentication or decryption has failed. ---> System.IO.IOException: The 
authentication or decryption has failed. ---> 
Mono.Security.Protocol.Tls.TlsException: The authentication or decryption has failed.

My code is as following;

public async Task<List<T>> GetList<T>(string url)
    {
        HttpClient client = new HttpClient();
        var response = await client.GetAsync(new Uri(url));
        response.EnsureSuccessStatusCode();
        var content = await response.Content.ReadAsStringAsync();
        return JsonConvert.DeserializeObject<List<T>>(content);
    }

The MainClass in android calls the core class, which calls this service class.

HttpClient requests work when the API does not use HTTPS, but throws this error whenever I use an API that has HTTPS enabled.

I'm also using a Portable Class Library to store code, so using HttpWebRequest for web requests isn't possible.

I have also tried;

ServicePointManager.ServerCertificateValidationCallback += (o, certificate, chain, errors) => true;

And;

System.Security.Cryptography.AesCryptoServiceProvider b = new System.Security.Cryptography.AesCryptoServiceProvider();

In my application OnCreate class (android).

I have also changed the HttpClient implementation to AndroidClientHandler, along with changing the SSL/TLS implementation to 1.1. Along with attempting this on an emulator and a device.

Does anyone have any advice?

2
  • 1
    Were you able to resolve this? I ran into the same issue and modifying the Android project worked for me. Under Android Options > Advanced, I set HttpClient implementation to Android and SSL/TLS implementation to Native TLS 1.2+ Commented Aug 6, 2017 at 20:54
  • I was also getting the same above error @Yanbin Hu good solution and apart from changing the HTTP client implementation from Default to Android native also changed SSL/TLS implementation from Default to - Native TLS 1.2+ - then it worked for me. Commented Dec 15, 2017 at 8:51

1 Answer 1

12

Try this: Open Android project property -> Android Options -> Advanced

Switch the HttpClient implementation setting from Default to Android

It seems that the default Mono client handler is broken for HTTPS.

Sign up to request clarification or add additional context in comments.

3 Comments

Will mention that leaving HttpClient Implementation as default, and updating TLS to be 1.2+ worked for me
Best solution. I don't know why these things need to be done. Is this standard one or just a hack?
Worked for me. I changed HttpClient implementation to Android. SSL/TLS implementation to Default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.