https取消证书验证

该代码片段展示了如何在Java中创建一个HTTP客户端,通过自定义SSL上下文并忽略证书验证来发送HTTPS请求。当遇到超时或一般异常时,会进行重试,如果多次失败则返回错误信息。
摘要由CSDN通过智能技术生成

96、https取消证书验证


    public static Map<String, String> getApi(String url, Map insertMap) throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
        // 创建SSL上下文,忽略证书验证
        SSLContextBuilder sslContextBuilder = SSLContexts.custom().loadTrustMaterial((chain, authType) -> true);
        SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build(), NoopHostnameVerifier.INSTANCE);

        Map<String, String> map = new HashMap<>();

        // 最多尝试请求次数
        int maxRetries = 3;
        int retry = 1;

        while (retry < maxRetries) {
// 发起请求
            try (CloseableHttpClient httpClient = HttpClients.custom()
                    .setSSLSocketFactory(sslSocketFactory)
                    .build()) {
//            try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
                // 创建 URIBuilder 对象
                URIBuilder uriBuilder = new URIBuilder(url);
                insertMap.forEach((k, v) -> {
                    // 添加参数
                    uriBuilder.addParameter(k.toString(), v.toString());
                });

                // 构建 URI
                URI newUrl = uriBuilder.build();
                System.out.println("查询内容" + newUrl.getQuery());
                // 创建 HttpGet 请求
                HttpGet httpGet = new HttpGet(newUrl);
                SslUtils.ignoreSsl();
                try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
                    HttpEntity entity = response.getEntity();
                    String responseBody = EntityUtils.toString(entity);
                    // 释放连接
                    EntityUtils.consume(entity);
                    map.put("code", "200");
                    map.put("message", "接口访问成功");
                    map.put("responseBody", responseBody);
                }
                return map;

            } catch (SocketTimeoutException e) {
                e.printStackTrace();
                map.put("code", "500");
                map.put("message", "接口访问超时");
            } catch (Exception e) {
                e.printStackTrace();
                map.put("code", "500");
                map.put("message", "接口访问失败");
            }
            try {
                Thread.sleep(20 * 1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            retry++;
        }

        // 如果超过最大重试次数仍然未成功,则直接返回错误信息
        if (retry == maxRetries) {
            map.put("code", "500");
            map.put("message", "尝试请求多次仍然失败,无法访问接口");
        }

        return map;
    }

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值