Android实现https网络通信之添加指定信任证书/信任所有证书

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

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

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

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


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

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

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public void initSSL() throws CertificateException, IOException, KeyStoreException,  
  2.             NoSuchAlgorithmException, KeyManagementException {  
  3.         CertificateFactory cf = CertificateFactory.getInstance("X.509");  
  4.         InputStream in = getAssets().open("ca.crt");  
  5.         Certificate ca = cf.generateCertificate(in);  
  6.   
  7.         KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());  
  8.         keystore.load(nullnull);  
  9.         keystore.setCertificateEntry("ca", ca);  
  10.   
  11.         String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();  
  12.         TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);  
  13.         tmf.init(keystore);  
  14.   
  15.         // Create an SSLContext that uses our TrustManager  
  16.         SSLContext context = SSLContext.getInstance("TLS");  
  17.         context.init(null, tmf.getTrustManagers(), null);  
  18.         URL url = new URL("https://certs.cac.washington.edu/CAtest/");  
  19. //        URL url = new URL("https://github.com");  
  20.         HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();  
  21.         urlConnection.setSSLSocketFactory(context.getSocketFactory());  
  22.         InputStream input = urlConnection.getInputStream();  
  23.   
  24.         BufferedReader reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));  
  25.         StringBuffer result = new StringBuffer();  
  26.         String line = "";  
  27.         while ((line = reader.readLine()) != null) {  
  28.             result.append(line);  
  29.         }  
  30.         Log.e("TTTT", result.toString());  
  31.     }  


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

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public void initSSLALL() throws KeyManagementException, NoSuchAlgorithmException, IOException {  
  2. //        URL url = new URL("https://certs.cac.washington.edu/CAtest/");  
  3.         URL url = new URL("https://github.com");  
  4.         SSLContext context = SSLContext.getInstance("TLS");  
  5.         context.init(nullnew TrustManager[]{new TrustAllManager()}, null);  
  6.         HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());  
  7.         HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {  
  8.   
  9.             @Override  
  10.             public boolean verify(String arg0, SSLSession arg1) {  
  11.                 return true;  
  12.             }  
  13.         });  
  14.         HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();  
  15.         connection.setDoInput(true);  
  16.         connection.setDoOutput(false);  
  17.         connection.setRequestMethod("GET");  
  18.         connection.connect();  
  19.         InputStream in = connection.getInputStream();  
  20.         BufferedReader reader = new BufferedReader(new InputStreamReader(in));  
  21.         String line = "";  
  22.         StringBuffer result = new StringBuffer();  
  23.         while ((line = reader.readLine()) != null) {  
  24.             result.append(line);  
  25.         }  
  26.         Log.e("TTTT", result.toString());  
  27.     }  
http://blog.csdn.net/whu_zhangmin/article/details/45868057
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值