Browse Source

漏洞修复

master
WendyYang 4 weeks ago
parent
commit
59eaa48a3b
1 changed files with 18 additions and 30 deletions
  1. +18
    -30
      kq-vas-api/src/main/java/com/ningdatech/kqapi/common/util/HttpUtil.java

+ 18
- 30
kq-vas-api/src/main/java/com/ningdatech/kqapi/common/util/HttpUtil.java View File

@@ -1,5 +1,7 @@
package com.ningdatech.kqapi.common.util; package com.ningdatech.kqapi.common.util;


import com.ningdatech.kqapi.common.exception.BizException;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair; import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig;
@@ -58,17 +60,19 @@ public class HttpUtil {
* @param httpUrl 地址 * @param httpUrl 地址
* @param maps 参数 * @param maps 参数
*/ */
public String sendHttpPost(String httpUrl, Map<String, String> maps) throws IOException {
HttpPost httpPost = new HttpPost(httpUrl);// 创建 httpPost
public String sendHttpPost(String httpUrl, Map<String, String> maps) {
// 创建 httpPost
HttpPost httpPost = new HttpPost(httpUrl);
// 创建参数队列 // 创建参数队列
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
for (String key : maps.keySet()) {
nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));
List<NameValuePair> nameValuePairs = new ArrayList<>();
for (Map.Entry<String, String> entry : maps.entrySet()) {
nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
} }
try { try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")); httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
} catch (Exception e) { } catch (Exception e) {
logger.error(e.getMessage()); logger.error(e.getMessage());
throw BizException.wrap("请求失败");
} }
return sendHttpPost(httpPost); return sendHttpPost(httpPost);
} }
@@ -76,39 +80,23 @@ public class HttpUtil {
/** /**
* 发送 Post请求 * 发送 Post请求
* *
* @param httpPost
* @return
* @param httpPost \
* @return \
*/ */
private String sendHttpPost(HttpPost httpPost) throws IOException {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
private String sendHttpPost(HttpPost httpPost) {
HttpEntity entity; HttpEntity entity;
String responseContent = null;
try {
httpPost.setConfig(requestConfig);
try (CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(httpPost);) {
// 创建默认的 httpClient 实例. // 创建默认的 httpClient 实例.
httpClient = HttpClients.createDefault();
httpPost.setConfig(requestConfig);
// 执行请求 // 执行请求
response = httpClient.execute(httpPost);

entity = response.getEntity(); entity = response.getEntity();
responseContent = EntityUtils.toString(entity, "UTF-8");
return EntityUtils.toString(entity, "UTF-8");
} catch (Exception e) { } catch (Exception e) {
logger.error(e.getMessage()); logger.error(e.getMessage());
} finally {
httpClient.close();
try {
// 关闭连接,释放资源
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
} catch (IOException e) {
logger.error(e.getMessage());
}
throw BizException.wrap("请求失败");
} }
return responseContent;
} }


} }

Loading…
Cancel
Save