记录 Java后台模拟请求https 被拦截提醒证书有问题

Java模拟HTTPS请求遇证书问题
   public IpcResponse sendPostRequest(String code) throws IOException {

        String result = "";
        CloseableHttpClient httpClient = null;
        try {
            // 创建 SSL 上下文构建器
            SSLContextBuilder builder = new SSLContextBuilder();
            // 全部信任 不做身份鉴定
            builder.loadTrustMaterial(null, new TrustStrategy() {
                @Override
                public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
                    return true;
                }
            });
            // 创建 SSL 连接套接字工厂
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(), new String[]{"TLSv1", "TLSv1.2"}, null, NoopHostnameVerifier.INSTANCE);
            // 注册连接套接字工厂
            org.apache.http.config.Registry<ConnectionSocketFactory> registry = org.apache.http.config.RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("http", new PlainConnectionSocketFactory())
                    .register("https", sslsf)
                    .build();
            // 创建连接池管理器
            PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
            cm.setMaxTotal(200); // 设置最大连接数
            // 创建 HttpClient 实例
            httpClient = HttpClients.custom()
                    .setSSLSocketFactory(sslsf)
                    .setConnectionManager(cm)
                    .setConnectionManagerShared(true)
                    .build();

            // 创建 HttpPost 请求对象
            HttpPost httpPost = new HttpPost("https://*******");

            // 假设需要提交表单数据,可以使用 UrlEncodedFormEntity 来构建表单数据
            // 这里只是示例,你可以根据实际需求修改

            List<NameValuePair> formParams = new ArrayList<>();
            formParams.add(new BasicNameValuePair("code", code));
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams, "UTF-8");
            httpPost.setEntity(entity);


            // 设置请求和传输超时时间
            RequestConfig requestConfig = RequestConfig.custom()
                    .setSocketTimeout(80000)
                    .setConnectTimeout(80000)
                    .build();
            httpPost.setConfig(requestConfig);

            // 执行请求
            HttpResponse httpResponse = httpClient.execute(httpPost);
            // 获取响应实体
            HttpEntity resEntity = httpResponse.getEntity();
            if (resEntity != null) {
                // 将响应实体内容转换为字符串
                result = EntityUtils.toString(resEntity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (httpClient != null) {
                    httpClient.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        IpcResponse ipcResponse = JSON.parseObject(result, IpcResponse.class);
        return ipcResponse;
    }
    public void fetchChildren(IpcNode parentNode) throws IOException, InterruptedException {
        IpcResponse response = sendPostRequest(parentNode.getCode());
        if (response.isSuccess() && response.getIpcList() != null && !response.getIpcList().isEmpty()) {
            for (IpcNode child : response.getIpcList()) {


                Ipc ipcs = BeanUtil.toBean(child, Ipc.class);
                save(ipcs);


                parentNode.addChild(child);
                Thread.sleep(1000);
                fetchChildren(child); // 递归调用,继续获取子节点的子节点
            }
        }
    }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值