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

时间:2019-10-14 11:45:43

标签: c# android xamarin ado.net

在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应用程序方面还很陌生,这是我似乎无法解决的问题。

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

3 个答案:

答案 0 :(得分:4)

//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. 

答案 1 :(得分:0)

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

在Forms中,创建一个界面

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将永远不会更新。

答案 2 :(得分:0)

卢卡斯的答案为我解决了!好答案。 只需在 MainActivity OnCreate 中注册服务即可:

public class MyEntityPk implements Serializable {

   private Integer aId;
   private Integer bId;

   // IMPORTANT: Override equals() and hashCode()
}


@IdClass(MyEntityPk.class)
@Entity
@Table(name = "my_entity")
public class MyEntity implements Serializable {

    @Id
    private Integer aId;
    @Id
    private Integer bId;

    @MapsId("aId")
    @ManyToOne
    private A a;

    @MapsId("bId")
    @OneToOne
    private B b;