From 73372f312e8b8bae98623006640b016f9a433988 Mon Sep 17 00:00:00 2001
From: nickchange <126672920+nickchange@users.noreply.github.com>
Date: 星期一, 16 十月 2023 15:12:49 +0800
Subject: [PATCH] 10.16
---
cloud-server-account/src/main/java/com/dsh/account/util/httpClinet/HttpClientUtil.java | 37 ++++++++++++++++++-------------------
1 files changed, 18 insertions(+), 19 deletions(-)
diff --git a/cloud-server-account/src/main/java/com/dsh/account/util/httpClinet/HttpClientUtil.java b/cloud-server-account/src/main/java/com/dsh/account/util/httpClinet/HttpClientUtil.java
index 2237d7c..dc08906 100644
--- a/cloud-server-account/src/main/java/com/dsh/account/util/httpClinet/HttpClientUtil.java
+++ b/cloud-server-account/src/main/java/com/dsh/account/util/httpClinet/HttpClientUtil.java
@@ -1,6 +1,7 @@
package com.dsh.account.util.httpClinet;
import com.alibaba.fastjson.JSON;
+import com.dsh.account.util.httpClinet.HttpResult;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
@@ -19,7 +20,6 @@
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.springframework.stereotype.Component;
import javax.net.ssl.SSLContext;
import java.io.File;
@@ -34,15 +34,14 @@
/**
* http工具类
*/
-@Component
public class HttpClientUtil {
private static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
- private PoolingHttpClientConnectionManager connectionManager;
+ private static PoolingHttpClientConnectionManager connectionManager;
- public HttpClientUtil(){
+ {
//1.创建连接池管理器
connectionManager = new PoolingHttpClientConnectionManager(60000,
TimeUnit.MILLISECONDS);
@@ -53,14 +52,14 @@
/**
* 创建一个httpClient对象
*/
- private CloseableHttpClient getHttpCline(){
+ private static CloseableHttpClient getHttpCline(){
return HttpClients.custom()
.setConnectionManager(connectionManager)
.disableAutomaticRetries()
.build();
}
- private RequestConfig getRequestConfig(){
+ private static RequestConfig getRequestConfig(){
RequestConfig.Builder builder = RequestConfig.custom();
builder.setSocketTimeout(60000)//3.1设置客户端等待服务端返回数据的超时时间
.setConnectTimeout(30000)//3.2设置客户端发起TCP连接请求的超时时间
@@ -76,9 +75,9 @@
* @param url 请求地址
* @param params 请求参数
*/
- private CloseableHttpResponse setPostHttpRequset(String url, Map<String, Object> params, Map<String, String> header, String contentType) throws Exception{
+ public static CloseableHttpResponse setPostHttpRequset(String url, Map<String, Object> params, Map<String, String> header, String contentType) throws Exception{
HttpPost httpPost = new HttpPost(url);
- httpPost.setConfig(this.getRequestConfig());
+ httpPost.setConfig(getRequestConfig());
if(null != header){
for(String key : header.keySet()){
httpPost.setHeader(key, header.get(key));
@@ -110,7 +109,7 @@
* @param url 请求地址
* @param params 请求参数
*/
- private CloseableHttpResponse setGetHttpRequset(String url, Map<String, Object> params, Map<String, String> header) throws Exception{
+ private static CloseableHttpResponse setGetHttpRequset(String url, Map<String, Object> params, Map<String, String> header) throws Exception{
StringBuffer sb = new StringBuffer();
String p = "";
if(null != params){
@@ -140,14 +139,14 @@
* @param contentType 参数请求方式form/json
* @return
*/
- public HttpResult pushHttpRequset(String mothed, String url, Map<String, Object> params, Map<String, String> header, String contentType) throws Exception{
+ public static HttpResult pushHttpRequset(String mothed, String url, Map<String, Object> params, Map<String, String> header, String contentType) throws Exception{
String randome = UUID.randomUUID().toString();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
logger.info(sdf.format(new Date()) + "----(" + randome + ")请求参数:" + JSON.toJSONString(params));
CloseableHttpResponse httpResponse = null;
switch (mothed){
case "GET":
- httpResponse = this.setGetHttpRequset(url, params, header);
+ httpResponse = setGetHttpRequset(url, params, header);
break;
case "POST":
httpResponse = setPostHttpRequset(url, params, header, contentType);
@@ -157,7 +156,7 @@
String content = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
logger.info(sdf.format(new Date()) + "----(" + randome + ")返回结果:" + content);
HttpResult httpResult = HttpResult.getHttpResult(statusCode, content);
- this.close(httpResponse);
+ close(httpResponse);
return httpResult;
}
@@ -169,7 +168,7 @@
* @param header 自定义请求头
* @return
*/
- public HttpResult pushHttpRequsetXml(String url, String xml, Map<String, String> header) throws Exception{
+ public static HttpResult pushHttpRequsetXml(String url, String xml, Map<String, String> header) throws Exception{
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(getRequestConfig());
for(String key : header.keySet()){
@@ -181,7 +180,7 @@
int statusCode = httpResponse.getStatusLine().getStatusCode();
String content = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
HttpResult httpResult = HttpResult.getHttpResult(statusCode, content);
- this.close(httpResponse);
+ close(httpResponse);
return httpResult;
}
@@ -198,14 +197,14 @@
* @return
* @throws Exception
*/
- public String pushHttpsRequsetXml(String url, String xml, Map<String, String> header, String certPassword, String certPath, String certType) throws Exception{
+ public static String pushHttpsRequsetXml(String url, String xml, Map<String, String> header, String certPassword, String certPath, String certType) throws Exception{
HttpPost httpPost = new HttpPost(url);
for(String key : header.keySet()){
httpPost.setHeader(key, header.get(key));
}
httpPost.setHeader("Content-Type", "application/xml");
httpPost.setEntity(new StringEntity(xml, "UTF-8"));
- CloseableHttpClient httpCline = this.initCert(certPassword, certPath, certType);
+ CloseableHttpClient httpCline = initCert(certPassword, certPath, certType);
CloseableHttpResponse httpResponse = httpCline.execute(httpPost);
String content = null;
if(httpResponse.getStatusLine().getStatusCode() == 200){
@@ -213,7 +212,7 @@
}else{
content = "返回状态码:" + httpResponse.getStatusLine() + "。" + EntityUtils.toString(httpResponse.getEntity());
}
- this.close(httpResponse);
+ close(httpResponse);
httpCline.close();
return content;
}
@@ -226,7 +225,7 @@
* @param certType 证书类型
* @throws Exception
*/
- private CloseableHttpClient initCert(String key, String certPath, String certType) throws Exception {
+ private static CloseableHttpClient initCert(String key, String certPath, String certType) throws Exception {
KeyStore keyStore = KeyStore.getInstance(certType);
InputStream inputStream = new FileInputStream(new File(certPath));
try {
@@ -246,7 +245,7 @@
/**
* 关闭资源
*/
- private void close(CloseableHttpResponse httpResponse){
+ private static void close(CloseableHttpResponse httpResponse){
try {
if(null != httpResponse){
EntityUtils.consume(httpResponse.getEntity());//此处高能,通过源码分析,由EntityUtils是否回收HttpEntity
--
Gitblit v1.7.1