HttpClientUtil.java 7.3 KB

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