柯桥增值式服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
7.2KB

  1. package com.ningdatech.kqapi.common.util;
  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 org.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19. import java.io.IOException;
  20. import java.net.URL;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Map;
  24. public class HttpUtil {
  25. private Logger logger = LoggerFactory.getLogger(getClass());
  26. private RequestConfig requestConfig = RequestConfig.custom()
  27. .setSocketTimeout(15000)
  28. .setConnectTimeout(15000)
  29. .setConnectionRequestTimeout(15000)
  30. .build();
  31. private static HttpUtil instance = null;
  32. private HttpUtil(){}
  33. public static HttpUtil getInstance(){
  34. if (instance == null) {
  35. instance = new HttpUtil();
  36. }
  37. return instance;
  38. }
  39. /**
  40. * 发送 post 请求
  41. * @param httpUrl 地址
  42. */
  43. public String sendHttpPost(String httpUrl) throws IOException {
  44. HttpPost httpPost= new HttpPost(httpUrl);// 创建 httpPost
  45. return sendHttpPost(httpPost);
  46. }
  47. /**
  48. * 发送 post 请求
  49. * @param httpUrl 地址
  50. * @param params 参数(格式:key1=value1&key2=value2)
  51. */
  52. public String sendHttpPost(String httpUrl, String params) throws IOException {
  53. HttpPost httpPost= new HttpPost(httpUrl);// 创建 httpPost
  54. try {
  55. //设置参数
  56. StringEntity stringEntity = new StringEntity(params, "UTF-8");
  57. stringEntity.setContentType("application/x-www-form-urlencoded");
  58. httpPost.setEntity(stringEntity);
  59. } catch (Exception e) {
  60. logger.error(e.getMessage());
  61. }
  62. return sendHttpPost(httpPost);
  63. }
  64. /**
  65. * 发送 post 请求
  66. * @param httpUrl 地址
  67. * @param maps 参数
  68. */
  69. public String sendHttpPost(String httpUrl, Map<String, String> maps) throws IOException {
  70. HttpPost httpPost= new HttpPost(httpUrl);// 创建 httpPost
  71. // 创建参数队列
  72. List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
  73. for (String key : maps.keySet()) {
  74. nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));
  75. }
  76. try {
  77. httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
  78. } catch (Exception e) {
  79. logger.error(e.getMessage());
  80. }
  81. return sendHttpPost(httpPost);
  82. }
  83. /**
  84. * 发送 Post请求
  85. * @param httpPost
  86. *@return
  87. */
  88. private String sendHttpPost(HttpPost httpPost) throws IOException {
  89. CloseableHttpClient httpClient = null;
  90. CloseableHttpResponse response = null;
  91. HttpEntity entity = null;
  92. String responseContent = null;
  93. try {
  94. // 创建默认的 httpClient 实例.
  95. httpClient = HttpClients.createDefault();
  96. httpPost.setConfig(requestConfig);
  97. // 执行请求
  98. response = httpClient.execute(httpPost);
  99. entity = response.getEntity();
  100. responseContent = EntityUtils.toString(entity, "UTF-8");
  101. } catch (Exception e) {
  102. logger.error(e.getMessage());
  103. } finally {
  104. httpClient.close();
  105. try {
  106. // 关闭连接,释放资源
  107. if (response != null) {
  108. response.close();
  109. }
  110. if (httpClient != null) {
  111. httpClient.close();
  112. }
  113. } catch (IOException e) {
  114. logger.error(e.getMessage());
  115. }
  116. }
  117. return responseContent;
  118. }
  119. /**
  120. * 发送 get 请求
  121. * @param httpUrl
  122. */
  123. public String sendHttpGet(String httpUrl) throws IOException {
  124. HttpGet httpGet = new HttpGet(httpUrl);// 创建 get 请求
  125. return sendHttpGet(httpGet);
  126. }
  127. /**
  128. * 发送 get请求 Https
  129. * @param httpUrl
  130. */
  131. public String sendHttpsGet(String httpUrl) {
  132. HttpGet httpGet = new HttpGet(httpUrl);// 创建 get 请求
  133. return sendHttpsGet(httpGet);
  134. }
  135. /**
  136. * 发送 Get请求
  137. * @param httpGet
  138. *@return
  139. */
  140. private String sendHttpGet(HttpGet httpGet) throws IOException {
  141. CloseableHttpClient httpClient = null;
  142. CloseableHttpResponse response = null;
  143. HttpEntity entity = null;
  144. String responseContent = null;
  145. try {
  146. // 创建默认的 httpClient 实例.
  147. httpClient = HttpClients.createDefault();
  148. httpGet.setConfig(requestConfig);
  149. // 执行请求
  150. response = httpClient.execute(httpGet);
  151. entity = response.getEntity();
  152. responseContent = EntityUtils.toString(entity, "UTF-8");
  153. } catch (Exception e) {
  154. logger.error(e.getMessage());
  155. } finally {
  156. httpClient.close();
  157. try {
  158. // 关闭连接,释放资源
  159. if (response != null) {
  160. response.close();
  161. }
  162. if (httpClient != null) {
  163. httpClient.close();
  164. }
  165. } catch (IOException e) {
  166. logger.error(e.getMessage());
  167. }
  168. }
  169. return responseContent;
  170. }
  171. /**
  172. * 发送 Get请求 Https
  173. *@return
  174. */
  175. private String sendHttpsGet(HttpGet httpGet) {
  176. CloseableHttpClient httpClient = null;
  177. CloseableHttpResponse response = null;
  178. HttpEntity entity = null;
  179. String responseContent = null;
  180. try {
  181. // 创建默认的 httpClient 实例.
  182. PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.load(new
  183. URL(httpGet.getURI().toString()));
  184. DefaultHostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(publicSuffixMatcher);
  185. httpClient = HttpClients.custom().setSSLHostnameVerifier(hostnameVerifier).build();
  186. httpGet.setConfig(requestConfig);
  187. // 执行请求
  188. response = httpClient.execute(httpGet);
  189. entity = response.getEntity();
  190. responseContent = EntityUtils.toString(entity, "UTF-8");
  191. } catch (Exception e) {
  192. logger.error(e.getMessage());
  193. } finally {
  194. try {
  195. // 关闭连接,释放资源
  196. if (response != null) {
  197. response.close();
  198. }
  199. if (httpClient != null) {
  200. httpClient.close();
  201. }
  202. } catch (IOException e) {
  203. logger.error(e.getMessage());
  204. }
  205. }
  206. return responseContent;
  207. }
  208. }