Xamarin:java.security.cert.CertPathValidatorException:未找到证书路径的信任锚

Dad*_* Mj 2 c# ado.net android xamarin

在 Xamarin 中。我一直在尝试在我的 Web API 和我的 Xamarin 项目之间进行通信。这是我的控制器的代码:

//  GET api/values
    public List<string> Get()
    {
        List<string> values = new List<string>();
        values.Add("Value 1");
        values.Add("Value 2");
        return values;
    }
Run Code Online (Sandbox Code Playgroud)

这是我在MainPage.xaml.cs 中的GET 请求

    public async void BindToListView()
    {
        HttpClient client = new HttpClient();
        var response = await client.GetStringAsync("https://10.0.2.2:#####/api/Values");
        var posts = JsonConvert.DeserializeObject<List<Posts>>(response);
        lv.ItemsSource = posts;
    }
Run Code Online (Sandbox Code Playgroud)

每当我尝试运行我的 Android 应用程序和我的 Web API 应用程序时。我不断收到此异常:

    Javax.Net.Ssl.SSLHandshakeException: 
'java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.'
Run Code Online (Sandbox Code Playgroud)

我试过将它粘贴到我的MainActivity.cs 中,但它仍然不起作用。

ServicePointManager.ServerCertificateValidationCallback += (o, cert, chain, errors) => true;
Run Code Online (Sandbox Code Playgroud)

我几个月来一直有这个问题,这让我发疯。我错过了什么吗?我是开发 Xamarin 应用程序的新手,这是一个我似乎无法解决的问题。

任何有关如何解决此问题的建议将不胜感激。感谢您花时间阅读本文。

小智 6

//Use this, it worked for me. 

HttpClient client;

public class datastore {
   var httpClientHandler = new HttpClientHandler();
            
   httpClientHandler.ServerCertificateCustomValidationCallback = 
   (message, cert, chain, errors) => { return true; };

   client = new HttpClient(httpClientHandler);
}

//... use the client to make request. it will bypass 
//the ssl certificates verification. 
Run Code Online (Sandbox Code Playgroud)

  • 谢谢。这是使用自签名证书进行本地测试的简单解决方案。 (2认同)