From 59eaa48a3be5829da03a71252bc772c6b105c97a Mon Sep 17 00:00:00 2001 From: WendyYang Date: Thu, 30 May 2024 13:34:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=BC=8F=E6=B4=9E=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ningdatech/kqapi/common/util/HttpUtil.java | 48 ++++++++-------------- 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/kq-vas-api/src/main/java/com/ningdatech/kqapi/common/util/HttpUtil.java b/kq-vas-api/src/main/java/com/ningdatech/kqapi/common/util/HttpUtil.java index 05c666b..f85af37 100644 --- a/kq-vas-api/src/main/java/com/ningdatech/kqapi/common/util/HttpUtil.java +++ b/kq-vas-api/src/main/java/com/ningdatech/kqapi/common/util/HttpUtil.java @@ -1,5 +1,7 @@ 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.NameValuePair; import org.apache.http.client.config.RequestConfig; @@ -58,17 +60,19 @@ public class HttpUtil { * @param httpUrl 地址 * @param maps 参数 */ - public String sendHttpPost(String httpUrl, Map maps) throws IOException { - HttpPost httpPost = new HttpPost(httpUrl);// 创建 httpPost + public String sendHttpPost(String httpUrl, Map maps) { + // 创建 httpPost + HttpPost httpPost = new HttpPost(httpUrl); // 创建参数队列 - List nameValuePairs = new ArrayList(); - for (String key : maps.keySet()) { - nameValuePairs.add(new BasicNameValuePair(key, maps.get(key))); + List nameValuePairs = new ArrayList<>(); + for (Map.Entry entry : maps.entrySet()) { + nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } try { httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")); } catch (Exception e) { logger.error(e.getMessage()); + throw BizException.wrap("请求失败"); } return sendHttpPost(httpPost); } @@ -76,39 +80,23 @@ public class HttpUtil { /** * 发送 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; - String responseContent = null; - try { + httpPost.setConfig(requestConfig); + try (CloseableHttpClient httpClient = HttpClients.createDefault(); + CloseableHttpResponse response = httpClient.execute(httpPost);) { // 创建默认的 httpClient 实例. - httpClient = HttpClients.createDefault(); - httpPost.setConfig(requestConfig); // 执行请求 - response = httpClient.execute(httpPost); + entity = response.getEntity(); - responseContent = EntityUtils.toString(entity, "UTF-8"); + return EntityUtils.toString(entity, "UTF-8"); } catch (Exception e) { 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; } } \ No newline at end of file