gistfile1.txt public class HttpUtils { public static final Logger logger = LoggerFactory.getLogger(HttpUtils.class); private static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(15000).setConnectTimeout(15000) .setCo
public class HttpUtils {
public static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
private static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(15000).setConnectTimeout(15000)
.setConnectionRequestTimeout(15000).build();
public static String sendHttpGet(HttpGet httpGet) {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
HttpEntity entity = null;
String responseContent = null;
try {
// 创建默认的httpClient实例.
httpClient = HttpClients.createDefault();
httpGet.setConfig(requestConfig);
// 执行请求
response = httpClient.execute(httpGet);
entity = response.getEntity();
responseContent = EntityUtils.toString(entity, "UTF-8");
} catch (Exception e) {
logger.error("sendHttpGet error:{}", e.getMessage());
} finally {
try {
// 关闭连接,释放资源
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
} catch (IOException e) {
logger.error("sendHttpGet IOException error:{}", e.getMessage());
}
}
return responseContent;
}
/**
* 发送 post请求
*
* @param httpUrl 地址
* @param maps 参数
*/
public static String sendHttpPost(String httpUrl, Map
maps) {
HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost
// 创建参数队列
List
nameValuePairs = new ArrayList
(); for (String key : maps.keySet()) { nameValuePairs.add(new BasicNameValuePair(key, maps.get(key))); } try { httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")); } catch (Exception e) { logger.error("sendHttpPost error:{}", e.getMessage()); } return sendHttpPost(httpPost); } /** * 发送 post请求 * * @param httpUrl 地址 * @param maps 参数 */ public static String sendHttpPostWithJson(String httpUrl, Map
maps) { HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost StringEntity entity = new StringEntity(JSONObject.toJSONString(maps), "UTF-8"); entity.setContentEncoding("UTF-8"); entity.setContentType("application/json"); try { httpPost.setEntity(entity); } catch (Exception e) { logger.error("sendHttpPost error:{}", e.getMessage()); } return sendHttpPost(httpPost); } public static 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) { logger.error("sendHttpPost error:{}", e.getMessage()); } finally { try { // 关闭连接,释放资源 if (response != null) { response.close(); } if (httpClient != null) { httpClient.close(); } } catch (IOException e) { logger.error("sendHttpPost IOException error:{}", e.getMessage()); } } return responseContent; } /** * 发送Get请求Https * * @param httpGet * @return */ public static String sendHttpsGet(HttpGet httpGet) { CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; HttpEntity entity = null; String responseContent = null; try { // 创建默认的httpClient实例. PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.load(new URL(httpGet.getURI().toString())); DefaultHostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(publicSuffixMatcher); httpClient = HttpClients.custom().setSSLHostnameVerifier(hostnameVerifier).build(); httpGet.setConfig(requestConfig); // 执行请求 response = httpClient.execute(httpGet); entity = response.getEntity(); responseContent = EntityUtils.toString(entity, "UTF-8"); } catch (Exception e) { logger.error("sendHttpsGet error:{}", e.getMessage()); } finally { try { // 关闭连接,释放资源 if (response != null) { response.close(); } if (httpClient != null) { httpClient.close(); } } catch (IOException e) { logger.error("sendHttpPost IOException error:{}", e.getMessage()); } } return responseContent; } private String getStringBuffer(HttpServletRequest request) { BufferedReader br = null; StringBuffer sb = null; try { //读取input流 br = new BufferedReader(new InputStreamReader((ServletInputStream) request.getInputStream(), "utf-8")); sb = new StringBuffer(""); String temp; while ((temp = br.readLine()) != null) { sb.append(temp); } } catch (IOException e) { } finally { try { br.close(); } catch (IOException e) { logger.error("e",e.getMessage()); } } return sb.toString(); } }
