HttpClientUtil.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package com.yihu.wlyy.util;
  2. import org.apache.http.HttpEntity;
  3. import org.apache.http.NameValuePair;
  4. import org.apache.http.ParseException;
  5. import org.apache.http.client.ClientProtocolException;
  6. import org.apache.http.client.entity.UrlEncodedFormEntity;
  7. import org.apache.http.client.methods.CloseableHttpResponse;
  8. import org.apache.http.client.methods.HttpGet;
  9. import org.apache.http.client.methods.HttpPost;
  10. import org.apache.http.impl.client.CloseableHttpClient;
  11. import org.apache.http.impl.client.HttpClients;
  12. import org.apache.http.message.BasicNameValuePair;
  13. import org.apache.http.util.EntityUtils;
  14. import org.json.JSONObject;
  15. import org.springframework.http.HttpHeaders;
  16. import org.springframework.http.MediaType;
  17. import org.springframework.stereotype.Component;
  18. import org.springframework.web.client.RestTemplate;
  19. import java.io.IOException;
  20. import java.io.UnsupportedEncodingException;
  21. import java.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.List;
  24. import java.util.Map;
  25. @Component
  26. public class HttpClientUtil {
  27. /**
  28. * 发送post请求
  29. *
  30. * @param url 请求地址
  31. * @param params 请求参数
  32. * @param chatSet 编码格式
  33. * @return
  34. */
  35. public String post(String url, List<NameValuePair> params, String chatSet) {
  36. // 创建默认的httpClient实例.
  37. CloseableHttpClient httpclient = HttpClients.createDefault();
  38. // 创建httppost
  39. HttpPost httppost = new HttpPost(url);
  40. UrlEncodedFormEntity uefEntity;
  41. try {
  42. uefEntity = new UrlEncodedFormEntity(params, chatSet);
  43. httppost.setEntity(uefEntity);
  44. CloseableHttpResponse response = httpclient.execute(httppost);
  45. try {
  46. HttpEntity entity = response.getEntity();
  47. if (entity != null) {
  48. return EntityUtils.toString(entity, chatSet);
  49. }
  50. } finally {
  51. response.close();
  52. }
  53. } catch (ClientProtocolException e) {
  54. e.printStackTrace();
  55. } catch (UnsupportedEncodingException e1) {
  56. e1.printStackTrace();
  57. } catch (IOException e) {
  58. e.printStackTrace();
  59. } finally {
  60. // 关闭连接,释放资源
  61. try {
  62. httpclient.close();
  63. } catch (IOException e) {
  64. e.printStackTrace();
  65. }
  66. }
  67. return null;
  68. }
  69. /**
  70. * 发送get请求
  71. *
  72. * @param url 请求地址
  73. * @param chatSet 编码格式
  74. * @return
  75. */
  76. public String get(String url, String chatSet) {
  77. CloseableHttpClient httpclient = HttpClients.createDefault();
  78. try {
  79. // 创建httpget.
  80. HttpGet httpget = new HttpGet(url);
  81. // 执行get请求.
  82. CloseableHttpResponse response = httpclient.execute(httpget);
  83. try {
  84. // 获取响应实体
  85. HttpEntity entity = response.getEntity();
  86. if (entity != null) {
  87. return EntityUtils.toString(entity, chatSet);
  88. }
  89. } finally {
  90. response.close();
  91. }
  92. } catch (ClientProtocolException e) {
  93. e.printStackTrace();
  94. } catch (ParseException e) {
  95. e.printStackTrace();
  96. } catch (IOException e) {
  97. e.printStackTrace();
  98. } finally {
  99. // 关闭连接,释放资源
  100. try {
  101. httpclient.close();
  102. } catch (IOException e) {
  103. e.printStackTrace();
  104. }
  105. }
  106. return null;
  107. }
  108. /**
  109. * http调用方法,(健康之路开放平台)
  110. *
  111. * @param url
  112. * @param params
  113. * @return
  114. * @throws Exception
  115. */
  116. public String httpPost(String url, Map<String, String> params) throws Exception {
  117. CloseableHttpClient httpclient = HttpClients.createDefault();
  118. try {
  119. HttpPost httpPost = new HttpPost(url);
  120. if (params != null && params.size() > 0) {
  121. List<NameValuePair> valuePairs = new ArrayList<NameValuePair>(params.size());
  122. for (Map.Entry<String, String> entry : params.entrySet()) {
  123. NameValuePair nameValuePair = new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue()));
  124. valuePairs.add(nameValuePair);
  125. }
  126. UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(valuePairs, "UTF-8");
  127. httpPost.setEntity(formEntity);
  128. }
  129. CloseableHttpResponse resp = httpclient.execute(httpPost);
  130. try {
  131. HttpEntity entity = resp.getEntity();
  132. String respContent = EntityUtils.toString(entity, "UTF-8").trim();
  133. return respContent;
  134. } finally {
  135. resp.close();
  136. }
  137. } catch (Exception e) {
  138. e.printStackTrace();
  139. return null;
  140. } finally {
  141. httpclient.close();
  142. }
  143. }
  144. /**
  145. * 获取加密后参数集合(健康之路开放平台)
  146. *
  147. * @param params
  148. * @return
  149. */
  150. public Map<String, String> getSecretParams(Map<String, String> params, String appId, String secret) {
  151. String timestamp = Long.toString(System.currentTimeMillis());
  152. params.put("timestamp", timestamp);
  153. StringBuilder stringBuilder = new StringBuilder();
  154. // 对参数名进行字典排序  
  155. String[] keyArray = params.keySet().toArray(new String[0]);
  156. Arrays.sort(keyArray);
  157. // 拼接有序的参数名-值串  
  158. stringBuilder.append(appId);
  159. for (String key : keyArray) {
  160. stringBuilder.append(key).append(params.get(key));
  161. }
  162. String codes = stringBuilder.append(secret).toString();
  163. String sign = org.apache.commons.codec.digest.DigestUtils.shaHex(codes).toUpperCase();
  164. // 添加签名,并发送请求  
  165. params.put("appId", appId);
  166. params.put("sign", sign);
  167. return params;
  168. }
  169. public String postBody(String url, JSONObject params) {
  170. RestTemplate restTemplate = new RestTemplate();
  171. HttpHeaders headers = new HttpHeaders();
  172. MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
  173. headers.setContentType(type);
  174. headers.add("Accept", MediaType.APPLICATION_JSON.toString());
  175. org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(params.toString(), headers);
  176. String ret = restTemplate.postForObject(url, formEntity, String.class);
  177. return ret;
  178. }
  179. public void putBody(String url, JSONObject params) {
  180. RestTemplate restTemplate = new RestTemplate();
  181. HttpHeaders headers = new HttpHeaders();
  182. MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
  183. headers.setContentType(type);
  184. headers.add("Accept", MediaType.APPLICATION_JSON.toString());
  185. org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(params.toString(), headers);
  186. restTemplate.put(url, formEntity, String.class);
  187. }
  188. }