HttpClientUtil.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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.config.RequestConfig;
  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.impl.client.CloseableHttpClient;
  12. import org.apache.http.impl.client.HttpClients;
  13. import org.apache.http.message.BasicNameValuePair;
  14. import org.apache.http.util.EntityUtils;
  15. import org.json.JSONObject;
  16. import org.springframework.http.HttpHeaders;
  17. import org.springframework.http.MediaType;
  18. import org.springframework.stereotype.Component;
  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.List;
  24. import java.util.Map;
  25. @Component
  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调用方法,(健康之路开放平台)
  70. *
  71. * @param url
  72. * @param params
  73. * @return
  74. * @throws Exception
  75. */
  76. public String httpPost(String url, Map<String, String> params) throws Exception {
  77. CloseableHttpClient httpclient = HttpClients.createDefault();
  78. RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(10000).build();//设置请求和传输超时时间
  79. try {
  80. HttpPost httpPost = new HttpPost(url);
  81. httpPost.setConfig(requestConfig);
  82. if (params != null && params.size() > 0) {
  83. List<NameValuePair> valuePairs = new ArrayList<NameValuePair>(params.size());
  84. for (Map.Entry<String, String> entry : params.entrySet()) {
  85. NameValuePair nameValuePair = new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue()));
  86. valuePairs.add(nameValuePair);
  87. }
  88. UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(valuePairs, "UTF-8");
  89. httpPost.setEntity(formEntity);
  90. }
  91. CloseableHttpResponse resp = httpclient.execute(httpPost);
  92. try {
  93. HttpEntity entity = resp.getEntity();
  94. String respContent = EntityUtils.toString(entity, "UTF-8").trim();
  95. return respContent;
  96. } finally {
  97. resp.close();
  98. }
  99. } catch (Exception e) {
  100. e.printStackTrace();
  101. return null;
  102. } finally {
  103. httpclient.close();
  104. }
  105. }
  106. /**
  107. * 发送get请求
  108. * @param url 请求地址
  109. * @param chatSet 编码格式
  110. * @return
  111. */
  112. public static String get(String url, String chatSet) {
  113. CloseableHttpClient httpclient = HttpClients.createDefault();
  114. try {
  115. // 创建httpget.
  116. HttpGet httpget = new HttpGet(url);
  117. // 执行get请求.
  118. CloseableHttpResponse response = httpclient.execute(httpget);
  119. try {
  120. // 获取响应实体
  121. HttpEntity entity = response.getEntity();
  122. if (entity != null) {
  123. return EntityUtils.toString(entity, chatSet);
  124. }
  125. } finally {
  126. response.close();
  127. }
  128. } catch (ClientProtocolException e) {
  129. e.printStackTrace();
  130. } catch (ParseException e) {
  131. e.printStackTrace();
  132. } catch (IOException e) {
  133. e.printStackTrace();
  134. } finally {
  135. // 关闭连接,释放资源
  136. try {
  137. httpclient.close();
  138. } catch (IOException e) {
  139. e.printStackTrace();
  140. }
  141. }
  142. return null;
  143. }
  144. public static String postBody(String url,JSONObject params){
  145. RestTemplate restTemplate = new RestTemplate();
  146. HttpHeaders headers = new HttpHeaders();
  147. MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
  148. headers.setContentType(type);
  149. headers.add("Accept", MediaType.APPLICATION_JSON.toString());
  150. org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(params.toString(), headers);
  151. String ret = restTemplate.postForObject(url, formEntity, String.class);
  152. return ret;
  153. }
  154. public static void putBody(String url,JSONObject params){
  155. RestTemplate restTemplate = new RestTemplate();
  156. HttpHeaders headers = new HttpHeaders();
  157. MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
  158. headers.setContentType(type);
  159. headers.add("Accept", MediaType.APPLICATION_JSON.toString());
  160. org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(params.toString(), headers);
  161. restTemplate.put(url, formEntity, String.class);
  162. }
  163. }