我在android中使用retrofit来连接服务器 .
public class ApiClient {
public static final String BASE_URL = "https://example.com/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
这是我的开发 . 服务器,我想禁用证书检查 . 我该如何在这段代码中实现?
错误:javax.net.ssl.SSLHandshakeException:java.security.cert.CertPathValidatorException:找不到证书路径的信任锚 .
5 回答
使用此类可以获得不安全的Retrofit实例 . 我已经包含了进口以避免混淆 .
然后简单地使用改造而不用像这样的ssl检查
I strongly discourage doing this
简短回答 - 子类HostNameVerifier,over-ride verify()总是返回true .
This has better options
答案很长 - 在这里查看我的(变得很旧)博客:Making Android and SSL Work Together
Maybe the best option for your scenario
将https删除到测试服务器的http,然后逻辑不必更改 .
HTH
IMO,你可以阅读Google's documentation - Security with HTTPS and SSL .
关于使用自签名证书使用Retrofit的示例代码,请尝试以下操作,希望它有所帮助!
自Hitesh Sahu的回答发布以来,语法发生了一些变化 . 现在,您可以将lambdas用于某些方法,删除一些throw子句和链构建器方法调用 .
在代码中实现此类变通方法,即使是出于测试目的也是一种不好的做法 .
您可以:
生成您的CA.
使用CA签署您的证书 .
将您的CA添加为受信任的 .
一些可能有用的链接:
https://jamielinux.com/docs/openssl-certificate-authority/create-the-root-pair.html
http://wiki.cacert.org/FAQ/ImportRootCert