zoukankan      html  css  js  c++  java
  • https绕过证书认证请求 Get或Post请求(证书过期,忽略证书)

    报错信息

    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    

    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

    1. 如果证书过期,配置容器为http请求,就可搞定。
    2. 如果postman请求遇到此情况:Could not get any response

    postman请求提示

    解决:

    postman方式

    1. 打开postman ---> File ---> Settings
      配置
    2. 关闭图片红框里的SSL..选项为OFF

    这样就可以了.

    java请求

    直接贴代码了。

    /**
         * 采用绕过验证的方式处理https请求
         * @param url
         * @param json
         * @return
         */
        public static String doGet(String url, JSONObject json) {
            String body = "";
            SSLContext sslcontext = null;
            try {
                //设置协议http和https对应的处理socket链接工厂的对象
                sslcontext = createIgnoreVerifySSL();
                Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                        .register("http", PlainConnectionSocketFactory.INSTANCE)
                        .register("https", new SSLConnectionSocketFactory(sslcontext))
                        .build();
                PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
                HttpClients.custom().setConnectionManager(connManager);
                //创建自定义的httpclient对象
                CloseableHttpClient client = HttpClients.custom().setConnectionManager(connManager).build();
                //处理请求参数拼接(参数为json) 如:?name = ""&pwd = ""
                String param = changeJsonToArguments(json);
                String urlNameString = url + param;
                //创建get方式请求对象
                HttpGet get = new HttpGet(urlNameString);
                //指定报文头Content-type、User-Agent
                //get.setHeader("Content-type", "application/x-www-form-urlencoded");
                get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2");
                //执行请求操作,并拿到结果(同步阻塞)
                CloseableHttpResponse response = client.execute(get);
                //获取结果实体
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    //按指定编码转换结果实体为String类型
                    body = EntityUtils.toString(entity, "UTF-8");
                }
                EntityUtils.consume(entity);
                //释放链接
                response.close();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (KeyManagementException e) {
                e.printStackTrace();
        }finally{
    
            }
            return body;
        }
    
        public static SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException {
            SSLContext sc = SSLContext.getInstance("SSLv3");
    
            // 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
            X509TrustManager trustManager = new X509TrustManager() {
                @Override
                public void checkClientTrusted(
                        java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
                        String paramString) {
                }
    
                @Override
                public void checkServerTrusted(
                        java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
                        String paramString) {
                }
    
                @Override
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
    
            sc.init(null, new TrustManager[] { trustManager }, null);
            return sc;
        }
    

    有帮助请留言...

    更多文章:www.ipooli.com

  • 相关阅读:
    前端性能优化:使用异步加载,延迟加载依赖
    超酷的图片标题悬浮特效
    JQuery插件:动态列和无间隙网格布局Mason.js
    如何高效地产生多个不重复的随机数?
    Endnote专题之--output style相关问题
    机器学习中的相似性度量(Similarity Measurement)
    C语言生成服从均匀分布, 瑞利分布, 莱斯分布, 高斯分布的随机数
    MathType中如何批量修改公式字体和大小
    LaTeX 算法代码排版 --latex2e范例总结
    Latex论文写作-Texsdudio 快捷键总结
  • 原文地址:https://www.cnblogs.com/ipoo/p/12785836.html
Copyright © 2011-2022 走看看