Home > front end >  Http Request work on Android Emulator but not in Android device - Xamarin cross-platform
Http Request work on Android Emulator but not in Android device - Xamarin cross-platform

Time:03-12

I have created an API and an application with Xamarin (cross-platform, for Android and iOS both). The first screen is a login page that perform an HTTP request to API. It works fine on Android Emulator, but not in Android device that I have connected with debug mode. I have already add the string '' in applicationhost.config and my operation are all async operation. Can i solve it?

Thank you a lot!!!

CodePudding user response:

It seems like you may be using HTTP instead of HTTPS. Newer versions of Android require you to take special steps to allow "insecure" HTTP only calls. See the documentation.

You definitely should use HTTPS in your final product in which case this wouldn't be necessary. In the meantime, you may be using a self-signed SSL certificate, add the android:usesCleartextTraffic XML attribute to your XML tag in the manifest:

<application android:usesCleartextTraffic="true" ... >

This answer goes into more detail as well.

CodePudding user response:

You could try to bypass the certificate using below code.

 HttpClientHandler clientHandler = new HttpClientHandler();
 clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };   

HttpClient client = new HttpClient(clientHandler)
  • Related