WebException in Xamarin using HttpClient

I am learning how to use xamarin with C #, and I have an Android app to make web requests to an external API, and I get 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 follows:

 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); } 

MainClass in android calls the main class that calls this service class.

HttpClient requests work when the API does not use HTTPS, but generates this error whenever I use the API with HTTPS enabled.

I also use a portable class library to store code, so using HttpWebRequest for web requests is not possible.

I 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 also changed the implementation of HttpClient to AndroidClientHandler, and also changed the SSL / TLS implementation to 1.1. Along with trying to do this on an emulator and device.

Does anyone have any tips?

+5
source share
1 answer

Try the following: Open the Android project property β†’ Android Settings β†’ Advanced

Toggle default HttpClient setting on Android

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

+10
source

Source: https://habr.com/ru/post/1263324/


All Articles