package com.nova.sankuai.infra.utils.huifubao.common; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.http.HttpEntity; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; public class HttpUtil { private RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(30000) .setConnectTimeout(30000) .setConnectionRequestTimeout(30000) .build(); /** * 发送 post请求 * * @param httpUrl 地址 * @param params 参数(Json格式) */ public String sendJsonHttpPost(String httpUrl, String params) { HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost try { //设置参数 StringEntity stringEntity = new StringEntity(params, "UTF-8"); stringEntity.setContentType("text/json"); httpPost.setEntity(stringEntity); } catch (Exception e) { e.printStackTrace(); } return sendHttpPost(httpPost); } /** * 发送Post请求 * * @param httpPost * @return */ public String sendHttpPost(HttpPost httpPost) { CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; HttpEntity entity = null; String responseContent = null; try { // 创建默认的httpClient实例. httpClient = HttpClients.createDefault(); httpPost.setConfig(requestConfig); // 执行请求 response = httpClient.execute(httpPost); entity = response.getEntity(); responseContent = EntityUtils.toString(entity, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } finally { try { // 关闭连接,释放资源 if (response != null) { response.close(); } if (httpClient != null) { httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } } return responseContent; } /** * 发送Post x-www-form-urlencoded请求 * * @param httpPost * @return */ public static String sendPost(String url, Map params){ CloseableHttpClient httpClient = null; HttpPost httpPost = null; CloseableHttpResponse response = null; String result = ""; try { httpClient = HttpClientBuilder.create().build(); httpPost = new HttpPost(url); // 设置参数 if (null != params && params.size() > 0){ List pairList = new ArrayList<>(); params.forEach((x,y) -> pairList.add(new BasicNameValuePair(x,y))); UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(pairList,"utf-8"); httpPost.setEntity(urlEncodedFormEntity); } response = httpClient.execute(httpPost); result = EntityUtils.toString(response.getEntity()); } catch (IOException e) { e.printStackTrace(); } return result; } public static JsonNode parseJsonResponse(String jsonResponse) throws Exception { ObjectMapper objectMapper = new ObjectMapper(); return objectMapper.readTree(jsonResponse); } }