I am facing the following exception when trying to POST to an https API using Open VPN
Note: When I try accessing the API using swagger in tablet on which Open VPN is connected, the API is connecting and working fine, it's just that the mobile app isn't able to consume it. ( in the Xamarin app we are getting the exception.
Code:
var uri = prepareLoginUri("/Login/Login");
var body = new { username, password };
var json = JsonConvert.SerializeObject(body);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await _client.PostAsync(uri, content);
Exception:
java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
Found a Solution for this exception but didn't fix the issue:
https://forums.xamarin.com/discussion/91782/trust-anchor-for-certification-path-not-found
According to the suggestion in this link, I added the following code in MainActivity.cs
HttpClientHandler clientHandler = new HttpClientHandler();
clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
_client = new HttpClient(clientHandler);
This solved the exception, but the response now I get from the API is 404 Not found.
StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent
but in swagger, the API is working absolutely fine.