httpClient 发送 https 请求错误验证失败,确保 SSL 证书问题较少
最编程
2024-07-15 11:16:05
...
HttpPost post = new HttpPost(url);
//创建CloseableHttpClient对象-忽略SSL证书
CloseableHttpClient client = null;
try {
SSLConnectionSocketFactory scsf = new SSLConnectionSocketFactory(
SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(),
NoopHostnameVerifier.INSTANCE);
client = HttpClients.custom().setSSLSocketFactory(scsf).build();
} catch (KeyManagementException e) {
logger.error("调用接口失败:",e);
} catch (NoSuchAlgorithmException e) {
logger.error("调用接口失败:",e);
} catch (KeyStoreException e) {
logger.error("调用接口失败:",e);
}
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(connectTimeout)
.setSocketTimeout(socketTimeout)
.build();
post.setConfig(requestConfig);
ByteArrayEntity entity = new ByteArrayEntity(data.getBytes(StandardCharsets.UTF_8));
entity.setContentType("application/json");
post.setEntity(entity);
CloseableHttpResponse res = client.execute(post);
下一篇: 未来潜艇会无人驾驶吗?