HttpClientUtils.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package com.template.common.utils;
  2. import org.apache.http.HttpEntity;
  3. import org.apache.http.NameValuePair;
  4. import org.apache.http.client.config.RequestConfig;
  5. import org.apache.http.client.entity.UrlEncodedFormEntity;
  6. import org.apache.http.client.methods.CloseableHttpResponse;
  7. import org.apache.http.client.methods.HttpGet;
  8. import org.apache.http.client.methods.HttpPost;
  9. import org.apache.http.conn.ssl.DefaultHostnameVerifier;
  10. import org.apache.http.conn.util.PublicSuffixMatcher;
  11. import org.apache.http.conn.util.PublicSuffixMatcherLoader;
  12. import org.apache.http.entity.StringEntity;
  13. import org.apache.http.impl.client.CloseableHttpClient;
  14. import org.apache.http.impl.client.HttpClients;
  15. import org.apache.http.message.BasicNameValuePair;
  16. import org.apache.http.util.EntityUtils;
  17. import java.io.IOException;
  18. import java.net.URL;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import java.util.Map;
  22. /**
  23. * <p>Title: HttpClientUtils</p>
  24. * <p>Description: httpClient 工具类</p>
  25. * @author fengyong
  26. * @date 2018年9月7日
  27. */
  28. public class HttpClientUtils {
  29. /**
  30. * 默认参数设置
  31. * setConnectTimeout:设置连接超时时间,单位毫秒。
  32. * setConnectionRequestTimeout:设置从connect Manager获取Connection 超时时间,单位毫秒。
  33. * setSocketTimeout:请求获取数据的超时时间,单位毫秒。访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。 暂时定义15分钟
  34. */
  35. private RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(600000).setConnectTimeout(600000).setConnectionRequestTimeout(600000).build();
  36. /**
  37. * 静态内部类---作用:单例产生类的实例
  38. * @author Administrator
  39. *
  40. */
  41. private static class LazyHolder {
  42. private static final HttpClientUtils INSTANCE = new HttpClientUtils();
  43. }
  44. private HttpClientUtils(){}
  45. public static HttpClientUtils getInstance(){
  46. return LazyHolder.INSTANCE;
  47. }
  48. /**
  49. * 发送 post请求
  50. * @param httpUrl 地址
  51. */
  52. public String sendHttpPost(String httpUrl) {
  53. HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost
  54. return sendHttpPost(httpPost);
  55. }
  56. /**
  57. * 发送 post请求
  58. * @param httpUrl 地址
  59. * @param params 参数(格式:key1=value1&key2=value2)
  60. */
  61. public String sendHttpPost(String httpUrl, String params) {
  62. HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost
  63. try {
  64. //设置参数
  65. StringEntity stringEntity = new StringEntity(params, "UTF-8");
  66. stringEntity.setContentType("application/x-www-form-urlencoded");
  67. httpPost.setEntity(stringEntity);
  68. } catch (Exception e) {
  69. e.printStackTrace();
  70. }
  71. return sendHttpPost(httpPost);
  72. }
  73. /**
  74. * 发送 post请求
  75. * @param httpUrl 地址
  76. * @param maps 参数
  77. */
  78. public String sendHttpPost(String httpUrl, Map<String, String> maps) {
  79. HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost
  80. // 创建参数队列
  81. List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
  82. for (String key : maps.keySet()) {
  83. nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));
  84. }
  85. try {
  86. httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
  87. } catch (Exception e) {
  88. e.printStackTrace();
  89. }
  90. return sendHttpPost(httpPost);
  91. }
  92. /**
  93. * 发送Post请求
  94. * @param httpPost
  95. * @return
  96. */
  97. private String sendHttpPost(HttpPost httpPost) {
  98. CloseableHttpClient httpClient = null;
  99. CloseableHttpResponse response = null;
  100. HttpEntity entity = null;
  101. String responseContent = null;
  102. try {
  103. // 创建默认的httpClient实例
  104. httpClient = HttpClients.createDefault();
  105. httpPost.setConfig(requestConfig);
  106. // 执行请求
  107. long execStart = System.currentTimeMillis();
  108. response = httpClient.execute(httpPost);
  109. long execEnd = System.currentTimeMillis();
  110. System.out.println("=================执行post请求耗时:"+(execEnd-execStart)+"ms");
  111. long getStart = System.currentTimeMillis();
  112. entity = response.getEntity();
  113. responseContent = EntityUtils.toString(entity, "UTF-8");
  114. long getEnd = System.currentTimeMillis();
  115. System.out.println("=================获取响应结果耗时:"+(getEnd-getStart)+"ms");
  116. } catch (Exception e) {
  117. e.printStackTrace();
  118. } finally {
  119. try {
  120. // 关闭连接,释放资源
  121. if (response != null) {
  122. response.close();
  123. }
  124. if (httpClient != null) {
  125. httpClient.close();
  126. }
  127. } catch (IOException e) {
  128. e.printStackTrace();
  129. }
  130. }
  131. return responseContent;
  132. }
  133. /**
  134. * 发送 get请求
  135. * @param httpUrl
  136. */
  137. public String sendHttpGet(String httpUrl) {
  138. HttpGet httpGet = new HttpGet(httpUrl);// 创建get请求
  139. return sendHttpGet(httpGet);
  140. }
  141. /**
  142. * 发送 get请求Https
  143. * @param httpUrl
  144. */
  145. public String sendHttpsGet(String httpUrl) {
  146. HttpGet httpGet = new HttpGet(httpUrl);// 创建get请求
  147. return sendHttpsGet(httpGet);
  148. }
  149. /**
  150. * 发送Get请求
  151. * @param httpPost
  152. * @return
  153. */
  154. private String sendHttpGet(HttpGet httpGet) {
  155. CloseableHttpClient httpClient = null;
  156. CloseableHttpResponse response = null;
  157. HttpEntity entity = null;
  158. String responseContent = null;
  159. try {
  160. // 创建默认的httpClient实例.
  161. httpClient = HttpClients.createDefault();
  162. httpGet.setConfig(requestConfig);
  163. // 执行请求
  164. response = httpClient.execute(httpGet);
  165. entity = response.getEntity();
  166. responseContent = EntityUtils.toString(entity, "UTF-8");
  167. } catch (Exception e) {
  168. e.printStackTrace();
  169. } finally {
  170. try {
  171. // 关闭连接,释放资源
  172. if (response != null) {
  173. response.close();
  174. }
  175. if (httpClient != null) {
  176. httpClient.close();
  177. }
  178. } catch (IOException e) {
  179. e.printStackTrace();
  180. }
  181. }
  182. return responseContent;
  183. }
  184. /**
  185. * 发送Get请求Https
  186. * @param httpPost
  187. * @return
  188. */
  189. private String sendHttpsGet(HttpGet httpGet) {
  190. CloseableHttpClient httpClient = null;
  191. CloseableHttpResponse response = null;
  192. HttpEntity entity = null;
  193. String responseContent = null;
  194. try {
  195. // 创建默认的httpClient实例.
  196. PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.load(new URL(httpGet.getURI().toString()));
  197. DefaultHostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(publicSuffixMatcher);
  198. httpClient = HttpClients.custom().setSSLHostnameVerifier(hostnameVerifier).build();
  199. httpGet.setConfig(requestConfig);
  200. // 执行请求
  201. response = httpClient.execute(httpGet);
  202. entity = response.getEntity();
  203. responseContent = EntityUtils.toString(entity, "UTF-8");
  204. } catch (Exception e) {
  205. e.printStackTrace();
  206. } finally {
  207. try {
  208. // 关闭连接,释放资源
  209. if (response != null) {
  210. response.close();
  211. }
  212. if (httpClient != null) {
  213. httpClient.close();
  214. }
  215. } catch (IOException e) {
  216. e.printStackTrace();
  217. }
  218. }
  219. return responseContent;
  220. }
  221. }