HttpUtils.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. package com.yihu.jw.care.util.http;
  2. import org.apache.http.HttpEntity;
  3. import org.apache.http.HttpStatus;
  4. import org.apache.http.NameValuePair;
  5. import org.apache.http.auth.AuthScope;
  6. import org.apache.http.auth.UsernamePasswordCredentials;
  7. import org.apache.http.client.CredentialsProvider;
  8. import org.apache.http.client.entity.UrlEncodedFormEntity;
  9. import org.apache.http.client.methods.*;
  10. import org.apache.http.entity.StringEntity;
  11. import org.apache.http.impl.client.BasicCredentialsProvider;
  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.springframework.util.StringUtils;
  17. import java.io.BufferedReader;
  18. import java.io.InputStream;
  19. import java.io.InputStreamReader;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.Map;
  23. /**
  24. * Utils - HTTP请求辅助工具类
  25. * Created by progr1mmer on 2017/9/27.
  26. */
  27. public class HttpUtils {
  28. public static HttpResponse doGet(String url, Map<String, Object> params) throws Exception {
  29. return doGet(url, params, null);
  30. }
  31. public static HttpResponse doGet(String url, Map<String, Object> params, Map<String, String> headers) throws Exception {
  32. return doGet(url, params, headers, null, null);
  33. }
  34. public static HttpResponse doGet(String url, Map<String, Object> params, Map<String, String> headers, String username, String password) throws Exception {
  35. String response;
  36. int status;
  37. CloseableHttpClient httpClient = null;
  38. CloseableHttpResponse closeableHttpResponse = null;
  39. List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
  40. if (params != null) {
  41. for (String key : params.keySet()) {
  42. Object value = params.get(key);
  43. if (value != null) {
  44. nameValuePairList.add(new BasicNameValuePair(key, String.valueOf(params.get(key))));
  45. }
  46. }
  47. }
  48. String paramStr = EntityUtils.toString(new UrlEncodedFormEntity(nameValuePairList, "UTF-8"));
  49. HttpGet httpGet = new HttpGet(url + "?" + paramStr);
  50. if (headers != null) {
  51. for (String key : headers.keySet()) {
  52. httpGet.addHeader(key, headers.get(key));
  53. }
  54. }
  55. try {
  56. if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
  57. UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(username, password);
  58. CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  59. credentialsProvider.setCredentials(AuthScope.ANY, usernamePasswordCredentials);
  60. httpClient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build();
  61. } else {
  62. httpClient = HttpClients.createDefault();
  63. }
  64. closeableHttpResponse = httpClient.execute(httpGet);
  65. HttpEntity resEntity = closeableHttpResponse.getEntity();
  66. status = closeableHttpResponse.getStatusLine().getStatusCode();
  67. response = getRespString(resEntity);
  68. } finally {
  69. try {
  70. if (closeableHttpResponse != null) {
  71. closeableHttpResponse.close();
  72. }
  73. if (httpClient != null) {
  74. httpClient.close();
  75. }
  76. }catch (Exception e) {
  77. e.printStackTrace();
  78. }
  79. }
  80. if (status != HttpStatus.SC_OK) {
  81. // LogService.getLogger().error(" GET: " + url + " " + status);
  82. }
  83. HttpResponse httpResponse = new HttpResponse(status, response);
  84. return httpResponse;
  85. }
  86. public static HttpResponse doPost(String url, Map<String, Object> params) throws Exception {
  87. return doPost(url, params, null);
  88. }
  89. public static HttpResponse doPost(String url, Map<String, Object> params, Map<String, String> headers) throws Exception{
  90. return doPost(url, params, headers, null, null);
  91. }
  92. public static HttpResponse doPost(String url, Map<String, Object> params, Map<String, String> headers, String username, String password) throws Exception{
  93. String response;
  94. int status;
  95. CloseableHttpClient httpClient = null;
  96. CloseableHttpResponse closeableHttpResponse = null;
  97. HttpPost httpPost = new HttpPost(url);
  98. List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
  99. if (params != null) {
  100. for (String key : params.keySet()) {
  101. Object value = params.get(key);
  102. if (value != null) {
  103. nameValuePairList.add(new BasicNameValuePair(key, String.valueOf(params.get(key))));
  104. }
  105. }
  106. }
  107. httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairList, "UTF-8"));
  108. if (headers != null) {
  109. for (String key : headers.keySet()) {
  110. httpPost.addHeader(key, headers.get(key));
  111. }
  112. }
  113. try {
  114. if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
  115. UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(username, password);
  116. CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  117. credentialsProvider.setCredentials(AuthScope.ANY, usernamePasswordCredentials);
  118. httpClient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build();
  119. } else {
  120. httpClient = HttpClients.createDefault();
  121. }
  122. closeableHttpResponse = httpClient.execute(httpPost);
  123. HttpEntity resEntity = closeableHttpResponse.getEntity();
  124. status = closeableHttpResponse.getStatusLine().getStatusCode();
  125. response = getRespString(resEntity);
  126. } finally {
  127. try {
  128. if (closeableHttpResponse != null) {
  129. closeableHttpResponse.close();
  130. }
  131. if (httpClient != null) {
  132. httpClient.close();
  133. }
  134. }catch (Exception e) {
  135. e.printStackTrace();
  136. }
  137. }
  138. if(status != HttpStatus.SC_OK) {
  139. // LogService.getLogger().error(" POST: " + url + " " + status);
  140. }
  141. HttpResponse httpResponse = new HttpResponse(status, response);
  142. return httpResponse;
  143. }
  144. public static HttpResponse doJsonPost(String url, String jsonData, Map<String, String> headers, String username, String password) throws Exception{
  145. String response;
  146. int status;
  147. CloseableHttpClient httpClient = null;
  148. CloseableHttpResponse closeableHttpResponse = null;
  149. HttpPost httpPost = new HttpPost(url);
  150. httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
  151. httpPost.setEntity(new StringEntity(jsonData, "UTF-8"));
  152. if (headers != null) {
  153. for (String key : headers.keySet()) {
  154. httpPost.addHeader(key, headers.get(key));
  155. }
  156. }
  157. try {
  158. if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
  159. UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(username, password);
  160. CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  161. credentialsProvider.setCredentials(AuthScope.ANY, usernamePasswordCredentials);
  162. httpClient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build();
  163. } else {
  164. httpClient = HttpClients.createDefault();
  165. }
  166. closeableHttpResponse = httpClient.execute(httpPost);
  167. HttpEntity resEntity = closeableHttpResponse.getEntity();
  168. status = closeableHttpResponse.getStatusLine().getStatusCode();
  169. response = getRespString(resEntity);
  170. } finally {
  171. try {
  172. if (closeableHttpResponse != null) {
  173. closeableHttpResponse.close();
  174. }
  175. if (httpClient != null) {
  176. httpClient.close();
  177. }
  178. } catch (Exception e) {
  179. e.printStackTrace();
  180. }
  181. }
  182. if(status != HttpStatus.SC_OK) {
  183. // LogService.getLogger().error(" POST: " + url + " " + status);
  184. }
  185. HttpResponse httpResponse = new HttpResponse(status, response);
  186. return httpResponse;
  187. }
  188. public static HttpResponse doPut(String url, Map<String, Object> params) throws Exception {
  189. return doPut(url, params, null);
  190. }
  191. public static HttpResponse doPut(String url, Map<String, Object> params, Map<String, String> headers) throws Exception {
  192. return doPut(url, params, headers, null, null);
  193. }
  194. public static HttpResponse doPut(String url, Map<String, Object> params, Map<String, String> headers, String username, String password) throws Exception {
  195. String response;
  196. int status;
  197. CloseableHttpClient httpClient = null;
  198. CloseableHttpResponse closeableHttpResponse = null;
  199. HttpPut httpPut = new HttpPut(url);
  200. List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
  201. if (params != null) {
  202. for (String key : params.keySet()) {
  203. Object value = params.get(key);
  204. if (value != null) {
  205. nameValuePairList.add(new BasicNameValuePair(key, String.valueOf(params.get(key))));
  206. }
  207. }
  208. }
  209. httpPut.setEntity(new UrlEncodedFormEntity(nameValuePairList, "UTF-8"));
  210. if (headers != null) {
  211. for (String key : headers.keySet()) {
  212. httpPut.addHeader(key, headers.get(key));
  213. }
  214. }
  215. try {
  216. if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
  217. UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(username, password);
  218. CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  219. credentialsProvider.setCredentials(AuthScope.ANY, usernamePasswordCredentials);
  220. httpClient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build();
  221. } else {
  222. httpClient = HttpClients.createDefault();
  223. }
  224. closeableHttpResponse = httpClient.execute(httpPut);
  225. HttpEntity resEntity = closeableHttpResponse.getEntity();
  226. status = closeableHttpResponse.getStatusLine().getStatusCode();
  227. response = getRespString(resEntity);
  228. } finally {
  229. try {
  230. if (closeableHttpResponse != null) {
  231. closeableHttpResponse.close();
  232. }
  233. if (httpClient != null) {
  234. httpClient.close();
  235. }
  236. } catch (Exception e) {
  237. e.printStackTrace();
  238. }
  239. }
  240. if (status != HttpStatus.SC_OK) {
  241. // LogService.getLogger().error(" PUT: " + url + " " + status);
  242. }
  243. HttpResponse httpResponse = new HttpResponse(status, response);
  244. return httpResponse;
  245. }
  246. public static HttpResponse doJsonPut(String url, String jsonData, Map<String, String> headers, String username, String password) throws Exception {
  247. String response;
  248. int status;
  249. CloseableHttpClient httpClient = null;
  250. CloseableHttpResponse closeableHttpResponse = null;
  251. HttpPut httpPut = new HttpPut(url);
  252. httpPut.setHeader("Content-Type", "application/json;charset=UTF-8");
  253. httpPut.setEntity(new StringEntity(jsonData, "UTF-8"));
  254. if (headers != null) {
  255. for (String key : headers.keySet()) {
  256. httpPut.addHeader(key, headers.get(key));
  257. }
  258. }
  259. try {
  260. if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
  261. UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(username, password);
  262. CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  263. credentialsProvider.setCredentials(AuthScope.ANY, usernamePasswordCredentials);
  264. httpClient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build();
  265. } else {
  266. httpClient = HttpClients.createDefault();
  267. }
  268. closeableHttpResponse = httpClient.execute(httpPut);
  269. HttpEntity resEntity = closeableHttpResponse.getEntity();
  270. status = closeableHttpResponse.getStatusLine().getStatusCode();
  271. response = getRespString(resEntity);
  272. } finally {
  273. try {
  274. if (closeableHttpResponse != null) {
  275. closeableHttpResponse.close();
  276. }
  277. if (httpClient != null) {
  278. httpClient.close();
  279. }
  280. } catch (Exception e) {
  281. e.printStackTrace();
  282. }
  283. }
  284. if (status != HttpStatus.SC_OK) {
  285. // LogService.getLogger().error(" PUT: " + url + " " + status);
  286. }
  287. HttpResponse httpResponse = new HttpResponse(status, response);
  288. return httpResponse;
  289. }
  290. public static HttpResponse doDelete(String url, Map<String, Object> params) throws Exception {
  291. return doDelete(url, params, null);
  292. }
  293. public static HttpResponse doDelete(String url, Map<String, Object> params, Map<String, String> headers) throws Exception {
  294. return doDelete(url, params, headers, null, null);
  295. }
  296. public static HttpResponse doDelete(String url, Map<String, Object> params, Map<String, String> headers, String username, String password) throws Exception {
  297. String response;
  298. int status;
  299. CloseableHttpClient httpClient = null;
  300. CloseableHttpResponse closeableHttpResponse = null;
  301. List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
  302. if (params != null) {
  303. for (String key : params.keySet()) {
  304. Object value = params.get(key);
  305. if (value != null) {
  306. nameValuePairList.add(new BasicNameValuePair(key, String.valueOf(params.get(key))));
  307. }
  308. }
  309. }
  310. String paramStr = EntityUtils.toString(new UrlEncodedFormEntity(nameValuePairList, "UTF-8"));
  311. HttpDelete httpDelete = new HttpDelete(url + "?" + paramStr);
  312. if (headers != null) {
  313. for (String key : headers.keySet()) {
  314. httpDelete.addHeader(key, headers.get(key));
  315. }
  316. }
  317. try {
  318. if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
  319. UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(username, password);
  320. CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  321. credentialsProvider.setCredentials(AuthScope.ANY, usernamePasswordCredentials);
  322. httpClient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build();
  323. } else {
  324. httpClient = HttpClients.createDefault();
  325. }
  326. closeableHttpResponse = httpClient.execute(httpDelete);
  327. HttpEntity resEntity = closeableHttpResponse.getEntity();
  328. status = closeableHttpResponse.getStatusLine().getStatusCode();
  329. response = getRespString(resEntity);
  330. } finally {
  331. try {
  332. if (closeableHttpResponse != null) {
  333. closeableHttpResponse.close();
  334. }
  335. if (httpClient != null) {
  336. httpClient.close();
  337. }
  338. }catch (Exception e) {
  339. e.printStackTrace();
  340. }
  341. }
  342. if (status != HttpStatus.SC_OK) {
  343. // LogService.getLogger().error(" DELETE: " + url + " " + status);
  344. }
  345. HttpResponse httpResponse = new HttpResponse(status, response);
  346. return httpResponse;
  347. }
  348. private static String getRespString(HttpEntity entity) throws Exception {
  349. if (entity == null) {
  350. return null;
  351. }
  352. InputStream is = entity.getContent();
  353. BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  354. StringBuilder stringBuilder = new StringBuilder();
  355. String line;
  356. while ((line = reader.readLine()) != null) {
  357. stringBuilder.append(line);
  358. }
  359. return stringBuilder.toString();
  360. }
  361. }