Java CloseableHttpClient https post请求 跳过证书验证 学习笔记
2022/1/18 17:06:37
本文主要是介绍Java CloseableHttpClient https post请求 跳过证书验证 学习笔记,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Java CloseableHttpClient https post请求 跳过证书验证
ps:打印日志用自己的日志打印的方法就好
导包
import java.io.IOException; import java.nio.charset.StandardCharsets; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.Map; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLContext; import org.apache.http.HttpEntity; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContextBuilder; import org.apache.http.ssl.TrustStrategy; import org.apache.http.util.EntityUtils;
代码
public static CloseableHttpClient createSSLClientDefault() { try { SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { // 信任所有 public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }).build(); HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE; SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, hostnameVerifier); return HttpClients.custom().setSSLSocketFactory(sslsf).build(); } catch (Exception e) { log.error("error", e); } return HttpClients.createDefault(); } public static String postJson(String reqUrl, String json, Map<String,String> headerMap) { String strResult = ""; CloseableHttpResponse response = null; CloseableHttpClient httpClient = null; if(reqUrl.startsWith("https")){ httpClient = createSSLClientDefault(); }else{ httpClient = HttpClients.custom() .setDefaultRequestConfig( RequestConfig.custom() .setSocketTimeout(1 * 60 * 1000) .setConnectTimeout(1000) .setConnectionRequestTimeout(1000) .build() ).build(); } HttpEntity httpEntity = null; try { HttpPost httpPost = new HttpPost(reqUrl); if(headerMap != null) { headerMap.forEach((k,v) -> httpPost.addHeader(k, v)); } StringEntity entity = new StringEntity(json, "UTF-8");//解决中文乱码问题 entity.setContentType("application/json"); httpPost.setEntity(entity); response = httpClient.execute(httpPost, HttpClientContext.create()); int status = response.getStatusLine().getStatusCode(); httpEntity = response.getEntity(); if (status == 200) { return EntityUtils.toString(httpEntity, StandardCharsets.UTF_8); } else { log.error(reqUrl + " 请求错误:\r\t" + EntityUtils.toString(httpEntity, StandardCharsets.UTF_8)); } return strResult; } catch (IOException e) { log.error(" 接口请求异常 ", e); } finally { try { if (httpEntity != null) { EntityUtils.consume(httpEntity); } if (response != null) { response.close(); } } catch (IOException e) { log.error(" 接口请求异常 ", e); } } return strResult; }
参考博客:
https://blog.csdn.net/weixin_41796956/article/details/90376504
https://blog.csdn.net/qy20115549/article/details/83373954
这篇关于Java CloseableHttpClient https post请求 跳过证书验证 学习笔记的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南