屏蔽HTTPS证书验证下载apk安装包

当我们从一个HTTPS的网址下载资源时,不需要经过它的证书验证实现下载想要的资源

 private  void  DownApk(String url) throws MalformedURLException {
        String urlStr=url;
        try {
// 目前的证书基本都是ca签名验证,SSL验证。
            SSLContext sc =SSLContext.getInstance("TLS");
            sc.init(null, new TrustManager[]{new MyTrustManager()}, new SecureRandom());
// HttpsURLConnection 验证,不是 HttpURLConnection 
        
 HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            HttpsURLConnection.setDefaultHostnameVerifier(new MyHostnameVerifier());
            HttpsURLConnection conn = (HttpsURLConnection)new URL(urlStr).openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
           
// 上面代码就可以做到屏蔽HTTPS的证书,从而实现通畅的下载资源。


 // 删除文件夹
            File apkfile = new File("/sdcard/xxx.apk");
            if (apkfile.exists()){
                apkfile.delete();
            }
            final String fileName = "xxx.apk";
            File tmpFile = new File("/sdcard/");
            if (!tmpFile.exists()) {
                tmpFile.mkdir();
            }
            downapkfile = new File("/sdcard/1jindianzi", fileName);
            try {
                downapkfile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            InputStream is = conn.getInputStream();
            //内容总长度
            long contentlength  =  conn.getContentLength();
            //已下载的长度
            long loadedlength = 0;
            System.out.println(contentlength+"");
            FileOutputStream fileOutput= new FileOutputStream(downapkfile);
            byte[] buf = new byte[1024];//分配byte
            conn.connect();
            double count = 0;
            if (conn.getResponseCode() >= 400) {

            } else {
                while (count <= 100) {
                    if (is != null) {
                        int numRead = is.read(buf);
                        if (numRead <= 0) {
                            sendMessage(1,0);
                            break;
                        } else {
 // 设置进度
                            fileOutput.write(buf, 0, numRead);
                            loadedlength += numRead;
                            int mprogress = (int) ((loadedlength*100)/contentlength);
// 设置下载进度,更新
//  设置通知发送消息给NOtifyManager进行更新(进度更新,状态更新),判断状态
                            System.out.println("ppp" + mprogress);
                            if(oldprocess <= mprogress-2 ){
// 避免notifymanager ANR,每下载百分之2才进行通知一次
                                oldprocess =mprogress;
                                sendMessage(0, mprogress);
                            }
                        }
                    } else {
                        break;
                    }
                }
            }

            conn.disconnect();//记得关闭连接
            fileOutput.close();
            is.close();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

你可能感兴趣的:(屏蔽HTTPS证书验证下载apk安装包)