Android5.0系统访问https要怎么做?
服务器是自签名的,我只要先要验证证书,我用了HttpUrlConnection和OkHttp这两种访问方式都不行,是我的方法有问题吗?
------解决思路----------------------
如果你用OKHttp可以使用下面的代码来设置https访问,强制通过验证
private void setHttpsSSL() {
try {
TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{tm}, null);
mOkHttpClient = mOkHttpClient.setSslSocketFactory(sslContext.getSocketFactory());
} catch (Exception e) {
LogUtil.d(new AssertionError().getMessage() + "");
}
}