【问题标题】:Xamarin: java.security.cert.CertPathValidatorException: Trust anchor for certification path not foundXamarin:java.security.cert.CertPathValidatorException:找不到证书路径的信任锚
【发布时间】:2019-10-14 11:45:43
【问题描述】:

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

这是我的 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;
    }

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

    Javax.Net.Ssl.SSLHandshakeException: 
'java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.'

我已尝试将其粘贴到我的 MainActivity.cs 中,但仍然无法正常工作。

ServicePointManager.ServerCertificateValidationCallback += (o, cert, chain, errors) => true;

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

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

【问题讨论】:

标签: c# android xamarin ado.net


【解决方案1】:
//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. 

【讨论】:

  • 谢谢。这是使用自签名证书进行本地测试的简单解决方案。
  • 谢谢,我正在为无法访问服务器的本地网络编写应用程序;使用自签名。非常适合调试我的应用程序!!!
【解决方案2】:

对于 Android,您应该做更多的事情。

在表单中,创建一个界面

public interface IHTTPClientHandlerCreationService
{
  HttpClientHandler GetInsecureHandler();
}

在Android中实现了接口:

[assembly: Dependency(typeof(HTTPClientHandlerCreationService_Android))]
namespace xxx.Droid
{
  public class HTTPClientHandlerCreationService_Android : CollateralUploader.Services.IHTTPClientHandlerCreationService
  {
    public HttpClientHandler GetInsecureHandler()
    {
      return new IgnoreSSLClientHandler();
    }
  }

  internal class IgnoreSSLClientHandler : AndroidClientHandler
  {
    protected override SSLSocketFactory ConfigureCustomSSLSocketFactory(HttpsURLConnection connection)
    {
      return SSLCertificateSocketFactory.GetInsecure(1000, null);
    }

    protected override IHostnameVerifier GetSSLHostnameVerifier(HttpsURLConnection connection)
    {
      return new IgnoreSSLHostnameVerifier();
    }
  }

  internal class IgnoreSSLHostnameVerifier : Java.Lang.Object, IHostnameVerifier
  {
    public bool Verify(string hostname, ISSLSession session)
    {
      return true;
    }
  }
}

当你调用方法Get

public async void BindToListView()
{
  HttpClient client;

  switch (Device.RuntimePlatform)
  {
    case Device.Android:
      this.httpClient = new HttpClient(DependencyService.Get<Services.IHTTPClientHandlerCreationService>().GetInsecureHandler());
      break;
    default:
      ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
      this.httpClient = new HttpClient(new HttpClientHandler());
      break;
  }

  var response = await client.GetStringAsync("https://10.0.2.2:#####/api/Values");
  var posts = JsonConvert.DeserializeObject<ObservableCollection<Posts>>(response);
  lv.ItemsSource = posts;
}

另外,我建议你可以使用ObservableCollection而不是List,因为它实现了接口INotifyPropertyChanged。否则 UI 将永远不会更新。

【讨论】:

  • iOS 也有实现吗?
猜你喜欢
  • 2014-01-29
  • 2017-10-03
  • 2019-05-19
  • 2016-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-08
  • 2017-04-28
相关资源
最近更新 更多