HttpTookitUtils.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package com.lightinit.hsdataportal.common;
  2. import java.io.IOException;
  3. import java.io.UnsupportedEncodingException;
  4. import java.util.List;
  5. import java.util.Map;
  6. import org.apache.http.HttpEntity;
  7. import org.apache.http.NameValuePair;
  8. import org.apache.http.ParseException;
  9. import org.apache.http.StatusLine;
  10. import org.apache.http.client.ClientProtocolException;
  11. import org.apache.http.client.entity.UrlEncodedFormEntity;
  12. import org.apache.http.client.methods.CloseableHttpResponse;
  13. import org.apache.http.client.methods.HttpGet;
  14. import org.apache.http.client.methods.HttpPost;
  15. import org.apache.http.entity.StringEntity;
  16. import org.apache.http.impl.client.CloseableHttpClient;
  17. import org.apache.http.impl.client.HttpClients;
  18. import org.apache.http.util.EntityUtils;
  19. //import org.apache.xmlbeans.impl.common.NameUtil;
  20. import org.springframework.util.StringUtils;
  21. /**
  22. * Created by Mr.Yao on 2017/5/4.
  23. */
  24. public class HttpTookitUtils {
  25. public static int getApplicationStatus(String url) {
  26. int statusCode=-1;
  27. CloseableHttpClient httpclient = HttpClients.createDefault();
  28. try {
  29. HttpGet httpget = new HttpGet(url);
  30. CloseableHttpResponse response = httpclient.execute(httpget);
  31. try {
  32. StatusLine statusLine = response.getStatusLine();
  33. statusCode= statusLine.getStatusCode();
  34. } finally {
  35. response.close();
  36. }
  37. } catch (ClientProtocolException e) {
  38. e.printStackTrace();
  39. } catch (ParseException e) {
  40. e.printStackTrace();
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. } finally {
  44. try {
  45. httpclient.close();
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. }
  49. }
  50. return statusCode;
  51. }
  52. public static String doGet(String url, List<NameValuePair> params, CharEncoding charset,String accept) {
  53. String responseContent="";
  54. CloseableHttpClient httpclient = HttpClients.createDefault();
  55. try {
  56. String queryParams="";
  57. if(params!= null) {
  58. for (NameValuePair pair : params) {
  59. if (queryParams.length() > 0) {
  60. queryParams += "&";
  61. }
  62. queryParams += pair.getName() + "=" + pair.getValue();
  63. }
  64. }
  65. if(StringUtils.isEmpty(queryParams)==false){
  66. if(url.contains("?")){
  67. url+="&";
  68. }else{
  69. url+="?";
  70. }
  71. url+=queryParams;
  72. }
  73. HttpGet httpget = new HttpGet(url);
  74. if(StringUtils.isEmpty(accept)==false)
  75. httpget.addHeader("Accept",accept);
  76. CloseableHttpResponse response = httpclient.execute(httpget);
  77. try {
  78. HttpEntity entity = response.getEntity();
  79. if (entity != null) {
  80. responseContent= EntityUtils.toString(entity, charset.value());
  81. }
  82. } finally {
  83. response.close();
  84. }
  85. } catch (ClientProtocolException e) {
  86. e.printStackTrace();
  87. } catch (ParseException e) {
  88. e.printStackTrace();
  89. } catch (IOException e) {
  90. e.printStackTrace();
  91. } finally {
  92. try {
  93. httpclient.close();
  94. } catch (IOException e) {
  95. e.printStackTrace();
  96. }
  97. }
  98. return responseContent;
  99. }
  100. public static String doGet(String url, List<NameValuePair> params, CharEncoding charset) {
  101. return doGet(url,params,charset,"");
  102. }
  103. public static String doPost(String url, List<NameValuePair> params, CharEncoding charset){
  104. return doPost(url,params,charset,"");
  105. }
  106. public static String doPost(String url, List<NameValuePair> params, CharEncoding charset,String accept) {
  107. String responseContent="";
  108. CloseableHttpClient httpclient = HttpClients.createDefault();
  109. HttpPost httppost = new HttpPost(url);
  110. if(StringUtils.isEmpty(accept)==false)
  111. httppost.addHeader("Accept",accept);
  112. UrlEncodedFormEntity uefEntity;
  113. try {
  114. uefEntity = new UrlEncodedFormEntity(params, charset.value());
  115. httppost.setEntity(uefEntity);
  116. CloseableHttpResponse response = httpclient.execute(httppost);
  117. try {
  118. HttpEntity entity = response.getEntity();
  119. if (entity != null) {
  120. responseContent=EntityUtils.toString(entity, charset.value());
  121. }
  122. } finally {
  123. response.close();
  124. }
  125. } catch (ClientProtocolException e) {
  126. e.printStackTrace();
  127. } catch (UnsupportedEncodingException e1) {
  128. e1.printStackTrace();
  129. } catch (IOException e) {
  130. e.printStackTrace();
  131. } finally {
  132. try {
  133. httpclient.close();
  134. } catch (IOException e) {
  135. e.printStackTrace();
  136. }
  137. }
  138. return responseContent;
  139. }
  140. public static String doPost(String url, List<NameValuePair> params, CharEncoding charset, Map<String,String> headers) {
  141. String responseContent="";
  142. CloseableHttpClient httpclient = HttpClients.createDefault();
  143. HttpPost httppost = new HttpPost(url);
  144. if(headers!=null) {
  145. for (Map.Entry<String, String> entry : headers.entrySet()) {
  146. httppost.addHeader(entry.getKey(), entry.getValue());
  147. }
  148. }
  149. UrlEncodedFormEntity uefEntity;
  150. try {
  151. uefEntity = new UrlEncodedFormEntity(params, charset.value());
  152. httppost.setEntity(uefEntity);
  153. CloseableHttpResponse response = httpclient.execute(httppost);
  154. try {
  155. HttpEntity entity = response.getEntity();
  156. if (entity != null) {
  157. responseContent=EntityUtils.toString(entity, charset.value());
  158. }
  159. } finally {
  160. response.close();
  161. }
  162. } catch (ClientProtocolException e) {
  163. e.printStackTrace();
  164. } catch (UnsupportedEncodingException e1) {
  165. e1.printStackTrace();
  166. } catch (IOException e) {
  167. e.printStackTrace();
  168. } finally {
  169. try {
  170. httpclient.close();
  171. } catch (IOException e) {
  172. e.printStackTrace();
  173. }
  174. }
  175. return responseContent;
  176. }
  177. public static String doPost(String url, String body, CharEncoding charset) {
  178. String responseContent="";
  179. CloseableHttpClient httpclient = HttpClients.createDefault();
  180. HttpPost httppost = new HttpPost(url);
  181. try {
  182. StringEntity uefEntity=new StringEntity(body, charset.value());
  183. httppost.setEntity(uefEntity);
  184. httppost.addHeader("Content-type","application/json");
  185. CloseableHttpResponse response = httpclient.execute(httppost);
  186. try {
  187. HttpEntity entity = response.getEntity();
  188. if (entity != null) {
  189. responseContent=EntityUtils.toString(entity, charset.value());
  190. }
  191. } finally {
  192. response.close();
  193. }
  194. } catch (ClientProtocolException e) {
  195. e.printStackTrace();
  196. } catch (UnsupportedEncodingException e1) {
  197. e1.printStackTrace();
  198. } catch (IOException e) {
  199. e.printStackTrace();
  200. } finally {
  201. try {
  202. httpclient.close();
  203. } catch (IOException e) {
  204. e.printStackTrace();
  205. }
  206. }
  207. return responseContent;
  208. }
  209. public static String doPost(String url, String body, CharEncoding charset,Map<String,String> headers) {
  210. String responseContent="";
  211. CloseableHttpClient httpclient = HttpClients.createDefault();
  212. HttpPost httppost = new HttpPost(url);
  213. try {
  214. StringEntity uefEntity=new StringEntity(body, charset.value());
  215. httppost.setEntity(uefEntity);
  216. if(headers!=null) {
  217. for (Map.Entry<String, String> entry : headers.entrySet()) {
  218. httppost.addHeader(entry.getKey(), entry.getValue());
  219. }
  220. }else {
  221. httppost.addHeader("Content-type", "application/json");
  222. }
  223. CloseableHttpResponse response = httpclient.execute(httppost);
  224. try {
  225. HttpEntity entity = response.getEntity();
  226. if (entity != null) {
  227. responseContent=EntityUtils.toString(entity, charset.value());
  228. }
  229. } finally {
  230. response.close();
  231. }
  232. } catch (ClientProtocolException e) {
  233. e.printStackTrace();
  234. } catch (UnsupportedEncodingException e1) {
  235. e1.printStackTrace();
  236. } catch (IOException e) {
  237. e.printStackTrace();
  238. } finally {
  239. try {
  240. httpclient.close();
  241. } catch (IOException e) {
  242. e.printStackTrace();
  243. }
  244. }
  245. return responseContent;
  246. }
  247. }