|
|
@@ -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<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 { |
|
|
|
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; |
|
|
|
} |
|
|
|
|
|
|
|
} |