HTTP请求三方接口绕过https证书检查

问题:在http请求https接口过程中经常会遇到SSL证书检查或者证书过期

** sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: validity check failed **
在这里插入图片描述

解决办法:绕过证书检查

代码如下:


public static byte[] getFile(String url) throws  Exception {
        log.info("get file url:"+url);
        try {
            /*HttpClientBuilder builder = HttpClients.custom();
            builder.setSSLHostnameVerifier((hostName, sslSession) -> {
                return true; // 证书校验通过
            });*/
            //CloseableHttpClient httpclient = builder.build();
            //CloseableHttpClient httpclient =  createSSLClientDefault();
            DefaultHttpClient httpclient = new DefaultHttpClient();
            enableSSL(httpclient);
            HttpGet request = new HttpGet(url);
            CloseableHttpResponse response = httpclient.execute(request);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                if(entity == null) return null;
                byte[] imgByte = EntityUtils.toByteArray(entity);
                return imgByte;
            }

        }catch (Exception ex){
            log.error("get file error:"+ex.getMessage());
            ex.printStackTrace();
            throw ex;
        }
        return null;
    }
    /**
     * 访问https的网站
     * @param httpclient
     */
    private static void enableSSL(DefaultHttpClient httpclient){
        //调用ssl
        try {
            SSLContext sslcontext = SSLContext.getInstance("TLS");
            sslcontext.init(null, new TrustManager[] { truseAllManager }, null);
            SSLSocketFactory sf = new SSLSocketFactory(sslcontext);
            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            Scheme https = new Scheme("https", sf, 443);
            httpclient.getConnectionManager().getSchemeRegistry().register(https);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 重写验证方法,取消检测ssl
     */
    private static TrustManager truseAllManager = new X509TrustManager(){

        public void checkClientTrusted(
                java.security.cert.X509Certificate[] arg0, String arg1)
                throws CertificateException {
            // TODO Auto-generated method stub

        }

        public void checkServerTrusted(
                java.security.cert.X509Certificate[] arg0, String arg1)
                throws CertificateException {
            // TODO Auto-generated method stub

        }

        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            // TODO Auto-generated method stub
            return null;
        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值