HttpClientUtil.java 6.0 KB

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