android https请求证书过滤白名单,Android实现https网络通信之添加指定信任证书/信任所有证书...

当Android客户端访问https网站,默认情况下,受证书信任限制,无法访问,可以有两种解决方法来实现:

1、将要访问的https网站的ca证书添加到客户端信任证书列表中,此种方式为谷歌推荐,安全性高。

2、将客户端设置为信任所有证书,也就是说不验证服务器证书,此种方式实现简单,但是安全性低,不推荐使用。

直接上代码,分别实现两种方式的访问。

1、客户端添加指定信任证书

assets目录中放置ca.crt证书,此证书为https://certs.cac.washington.edu/CAtest/网站的信任证书。

CODE_ico.pngico_fork.svg

public void initSSL() throws CertificateException, IOException, KeyStoreException,

NoSuchAlgorithmException, KeyManagementException {

CertificateFactory cf = CertificateFactory.getInstance("X.509");

InputStream in = getAssets().open("ca.crt");

Certificate ca = cf.generateCertificate(in);

KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());

keystore.load(null, null);

keystore.setCertificateEntry("ca", ca);

String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();

TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);

tmf.init(keystore);

// Create an SSLContext that uses our TrustManager

SSLContext context = SSLContext.getInstance("TLS");

context.init(null, tmf.getTrustManagers(), null);

URL url = new URL("https://certs.cac.washington.edu/CAtest/");

//        URL url = new URL("https://github.com");

HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();

urlConnection.setSSLSocketFactory(context.getSocketFactory());

InputStream input = urlConnection.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));

StringBuffer result = new StringBuffer();

String line = "";

while ((line = reader.readLine()) != null) {

result.append(line);

}

Log.e("TTTT", result.toString());

}

2、客户端信任所有https,免证书验证

CODE_ico.pngico_fork.svg

public void initSSLALL() throws KeyManagementException, NoSuchAlgorithmException, IOException {

//        URL url = new URL("https://certs.cac.washington.edu/CAtest/");

URL url = new URL("https://github.com");

SSLContext context = SSLContext.getInstance("TLS");

context.init(null, new TrustManager[]{new TrustAllManager()}, null);

HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());

HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

@Override

public boolean verify(String arg0, SSLSession arg1) {

return true;

}

});

HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();

connection.setDoInput(true);

connection.setDoOutput(false);

connection.setRequestMethod("GET");

connection.connect();

InputStream in = connection.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(in));

String line = "";

StringBuffer result = new StringBuffer();

while ((line = reader.readLine()) != null) {

result.append(line);

}

Log.e("TTTT", result.toString());

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值