HttpClientUtil.java 7.2 KB

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