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); // 递归调用,继续获取子节点的子节点
}
}
}
记录 Java后台模拟请求https 被拦截提醒证书有问题
于 2025-02-09 15:52:31 首次发布
171万+

被折叠的 条评论
为什么被折叠?



