0

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.

Chetan Rawat
  • 578
  • 3
  • 17
Praveen Rao Chavan.G
  • 2,772
  • 3
  • 22
  • 33

2 Answers2

0

Solution

We need an SSL Certificate, but even after adding one we had this issue but this had nothing to do with the Mobile app, the problem was the SSL Certificate we had not configured the certificate properly, we had to add the intermediate certificates in generating the .pem file for HA Proxy.

This website shifted our focus from the mobile app to the server. https://www.digicert.com/help/

It clearly said the issue is with the server, the following is the link which helped me solve this issue.

Trust Anchor not found for Android SSL Connection

Praveen Rao Chavan.G
  • 2,772
  • 3
  • 22
  • 33
-1

Try to place a slash(/)at the end of the BaseAddress, and you must not place a slash(/) at the beginning of your relative URI. like :

client.BaseAddress = new Uri("http://192.168.1.100:33435/ManageSystem/");
var response = await client.GetAsync("Login/Login");
Leo Zhu
  • 15,726
  • 1
  • 7
  • 23