HttpClientUtil.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. public static String post(String url, List<NameValuePair> params, String chatSet,String userAgent) {
  69. // 创建默认的httpClient实例.
  70. CloseableHttpClient httpclient = HttpClients.createDefault();
  71. // 创建httppost
  72. HttpPost httppost = new HttpPost(url);
  73. UrlEncodedFormEntity uefEntity;
  74. try {
  75. uefEntity = new UrlEncodedFormEntity(params, chatSet);
  76. httppost.setEntity(uefEntity);
  77. httppost.setHeader("user-agent",userAgent);
  78. CloseableHttpResponse response = httpclient.execute(httppost);
  79. try {
  80. HttpEntity entity = response.getEntity();
  81. if (entity != null) {
  82. return EntityUtils.toString(entity, chatSet);
  83. }
  84. } finally {
  85. response.close();
  86. }
  87. } catch (ClientProtocolException e) {
  88. e.printStackTrace();
  89. } catch (UnsupportedEncodingException e1) {
  90. e1.printStackTrace();
  91. } catch (IOException e) {
  92. e.printStackTrace();
  93. } finally {
  94. // 关闭连接,释放资源
  95. try {
  96. httpclient.close();
  97. } catch (IOException e) {
  98. e.printStackTrace();
  99. }
  100. }
  101. return null;
  102. }
  103. /**
  104. * http post调用方法
  105. * @param url
  106. * @param xmlData 格式字符串
  107. * @return
  108. * @throws Exception
  109. */
  110. public String httpPost(String url, String xmlData)throws Exception {
  111. CloseableHttpClient httpclient = HttpClients.createDefault();
  112. String strResult="";
  113. try {
  114. HttpPost post = new HttpPost(url);
  115. StringEntity entity = new StringEntity(xmlData);
  116. post.setEntity(entity);
  117. post.setHeader("Content-Type", "text/xml;charset=UTF-8");
  118. HttpResponse response = httpclient.execute(post);
  119. if(response.getStatusLine().getStatusCode()==200){
  120. try {
  121. // 读取服务器返回过来的json字符串数据
  122. strResult = EntityUtils.toString(response.getEntity(),"UTF-8");
  123. } catch (Exception e) {
  124. e.printStackTrace();
  125. }
  126. }
  127. } catch (ClientProtocolException e) {
  128. e.printStackTrace();
  129. } catch (ParseException e) {
  130. e.printStackTrace();
  131. } catch (IOException e) {
  132. e.printStackTrace();
  133. } finally {
  134. // 关闭连接,释放资源
  135. try {
  136. httpclient.close();
  137. } catch (IOException e) {
  138. e.printStackTrace();
  139. }
  140. }
  141. return strResult;
  142. }
  143. /**
  144. * 发送get请求
  145. * @param url 请求地址
  146. * @param chatSet 编码格式
  147. * @return
  148. */
  149. public static String get(String url, String chatSet) {
  150. CloseableHttpClient httpclient = HttpClients.createDefault();
  151. try {
  152. // 创建httpget.
  153. HttpGet httpget = new HttpGet(url);
  154. // 执行get请求.
  155. CloseableHttpResponse response = httpclient.execute(httpget);
  156. try {
  157. // 获取响应实体
  158. HttpEntity entity = response.getEntity();
  159. if (entity != null) {
  160. return EntityUtils.toString(entity, chatSet);
  161. }
  162. } finally {
  163. response.close();
  164. }
  165. } catch (ClientProtocolException e) {
  166. e.printStackTrace();
  167. } catch (ParseException e) {
  168. e.printStackTrace();
  169. } catch (IOException e) {
  170. e.printStackTrace();
  171. } finally {
  172. // 关闭连接,释放资源
  173. try {
  174. httpclient.close();
  175. } catch (IOException e) {
  176. e.printStackTrace();
  177. }
  178. }
  179. return null;
  180. }
  181. /**
  182. * http调用方法,(健康之路开放平台)
  183. * @param url
  184. * @param params
  185. * @return
  186. * @throws Exception
  187. */
  188. public static String httpPost(String url, Map<String, String> params) throws Exception {
  189. CloseableHttpClient httpclient = HttpClients.createDefault();
  190. try {
  191. HttpPost httpPost = new HttpPost(url);
  192. if(params!=null&&params.size()>0){
  193. List<NameValuePair> valuePairs = new ArrayList<NameValuePair>(params.size());
  194. for (Map.Entry<String, String> entry : params.entrySet()) {
  195. NameValuePair nameValuePair = new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue()));
  196. valuePairs.add(nameValuePair);
  197. }
  198. UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity( valuePairs, "UTF-8");
  199. httpPost.setEntity(formEntity);
  200. }
  201. CloseableHttpResponse resp = httpclient.execute(httpPost);
  202. try {
  203. HttpEntity entity = resp.getEntity();
  204. String respContent = EntityUtils.toString(entity, "UTF-8").trim();
  205. return respContent;
  206. } finally {
  207. resp.close();
  208. }
  209. } catch (Exception e) {
  210. e.printStackTrace();
  211. return null;
  212. }finally {
  213. httpclient.close();
  214. }
  215. }
  216. /**
  217. * 获取加密后参数集合(健康之路开放平台)
  218. * @param params
  219. * @return
  220. */
  221. public static Map<String,String> getSecretParams(Map<String, String> params,String appId,String secret)
  222. {
  223. String timestamp = Long.toString(System.currentTimeMillis());
  224. params.put("timestamp", timestamp);
  225. StringBuilder stringBuilder = new StringBuilder();
  226. // 对参数名进行字典排序  
  227. String[] keyArray = params.keySet().toArray(new String[0]);
  228. Arrays.sort(keyArray);
  229. // 拼接有序的参数名-值串  
  230. stringBuilder.append(appId);
  231. for(String key : keyArray)
  232. {
  233. stringBuilder.append(key).append(params.get(key));
  234. }
  235. String codes = stringBuilder.append(secret).toString();
  236. String sign = org.apache.commons.codec.digest.DigestUtils.shaHex(codes).toUpperCase();
  237. // 添加签名,并发送请求  
  238. params.put("appId", appId);
  239. params.put("sign", sign);
  240. return params;
  241. }
  242. public static String postBody(String url,JSONObject params){
  243. RestTemplate restTemplate = new RestTemplate();
  244. HttpHeaders headers = new HttpHeaders();
  245. MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
  246. headers.setContentType(type);
  247. headers.add("Accept", MediaType.APPLICATION_JSON.toString());
  248. org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(params.toString(), headers);
  249. String ret = restTemplate.postForObject(url, formEntity, String.class);
  250. return ret;
  251. }
  252. public static void putBody(String url,JSONObject params){
  253. RestTemplate restTemplate = new RestTemplate();
  254. HttpHeaders headers = new HttpHeaders();
  255. MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
  256. headers.setContentType(type);
  257. headers.add("Accept", MediaType.APPLICATION_JSON.toString());
  258. org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(params.toString(), headers);
  259. restTemplate.put(url, formEntity, String.class);
  260. }
  261. }