HttpUtil.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. package com.ym.mec.util.http;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.ym.mec.util.compress.ZipUtil;
  4. import org.apache.commons.beanutils.ConvertUtils;
  5. import org.apache.commons.io.FileUtils;
  6. import org.apache.commons.io.IOUtils;
  7. import org.apache.commons.lang3.StringUtils;
  8. import org.apache.http.HttpEntity;
  9. import org.apache.http.HttpResponse;
  10. import org.apache.http.NameValuePair;
  11. import org.apache.http.client.entity.UrlEncodedFormEntity;
  12. import org.apache.http.client.methods.HttpGet;
  13. import org.apache.http.client.methods.HttpPost;
  14. import org.apache.http.entity.ContentType;
  15. import org.apache.http.entity.StringEntity;
  16. import org.apache.http.entity.mime.MultipartEntityBuilder;
  17. import org.apache.http.impl.client.CloseableHttpClient;
  18. import org.apache.http.impl.client.HttpClients;
  19. import org.apache.http.message.BasicNameValuePair;
  20. import org.apache.http.util.EntityUtils;
  21. import java.io.File;
  22. import java.io.FileOutputStream;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.net.URLDecoder;
  26. import java.nio.charset.Charset;
  27. import java.security.KeyManagementException;
  28. import java.security.KeyStoreException;
  29. import java.security.NoSuchAlgorithmException;
  30. import java.util.ArrayList;
  31. import java.util.HashMap;
  32. import java.util.List;
  33. import java.util.Map;
  34. import java.util.Map.Entry;
  35. public class HttpUtil {
  36. public static String getSortUrl(String url){
  37. try {
  38. Map<String,Object> paramMap = new HashMap<>();
  39. paramMap.put("format","json");
  40. paramMap.put("url",URLDecoder.decode(url,"UTF-8"));
  41. paramMap.put("key","5dc941c5d3c3816ac84898d7@3d0e03b46a30f4fea51f038e5cd411c5");
  42. String s = get("http://mrw.so/api.htm", paramMap);
  43. String shortUrl=JSONObject.parseObject(s).getString("url");
  44. if(StringUtils.isNotBlank(shortUrl)){
  45. return shortUrl;
  46. }else{
  47. return url;
  48. }
  49. }catch (Exception e){
  50. return url;
  51. }
  52. }
  53. /**
  54. * POST请求http url
  55. *
  56. * @param url
  57. * URL
  58. * @param parameterMap
  59. * 请求参数
  60. * @return 返回结果
  61. * @throws IOException
  62. */
  63. public static String postForHttp(String url, String json, Map<String, String> headers) throws IOException {
  64. String result = null;
  65. CloseableHttpClient httpClient = HttpClients.createDefault();
  66. HttpPost httpPost = new HttpPost(url);
  67. if (headers != null && headers.size() > 0) {
  68. for (Entry<String, String> entry : headers.entrySet()) {
  69. httpPost.setHeader(entry.getKey(), entry.getValue());
  70. }
  71. }
  72. try {
  73. httpPost.setEntity(new StringEntity(json, "UTF-8"));
  74. HttpResponse httpResponse = httpClient.execute(httpPost);
  75. HttpEntity httpEntity = httpResponse.getEntity();
  76. result = EntityUtils.toString(httpEntity, "UTF-8");
  77. EntityUtils.consume(httpEntity);
  78. } finally {
  79. httpPost.releaseConnection();
  80. httpClient.close();
  81. }
  82. return result;
  83. }
  84. /**
  85. * POST请求http url
  86. *
  87. * @param url
  88. * URL
  89. * @param parameterMap
  90. * 请求参数
  91. * @return 返回结果
  92. * @throws IOException
  93. */
  94. public static String postForHttp(String url, Map<String, Object> parameterMap) throws IOException {
  95. String result = null;
  96. CloseableHttpClient httpClient = HttpClients.createDefault();
  97. HttpPost httpPost = new HttpPost(url);
  98. try {
  99. List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
  100. if (parameterMap != null) {
  101. for (Entry<String, Object> entry : parameterMap.entrySet()) {
  102. String name = entry.getKey();
  103. String value = ConvertUtils.convert(entry.getValue());
  104. if (StringUtils.isNotEmpty(name)) {
  105. nameValuePairs.add(new BasicNameValuePair(name, value));
  106. }
  107. }
  108. }
  109. httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
  110. HttpResponse httpResponse = httpClient.execute(httpPost);
  111. HttpEntity httpEntity = httpResponse.getEntity();
  112. result = EntityUtils.toString(httpEntity, "UTF-8");
  113. EntityUtils.consume(httpEntity);
  114. } finally {
  115. httpPost.releaseConnection();
  116. httpClient.close();
  117. }
  118. return result;
  119. }
  120. /**
  121. * POST请求https url
  122. *
  123. * @param url
  124. * URL
  125. * @param parameterMap
  126. * 请求参数
  127. * @return 返回结果
  128. * @throws IOException
  129. * @throws NoSuchAlgorithmException
  130. * @throws KeyManagementException
  131. * @throws KeyStoreException
  132. */
  133. public static String postForHttps(String url, Map<String, Object> parameterMap) throws IOException, NoSuchAlgorithmException, KeyManagementException,
  134. KeyStoreException {
  135. String result = null;
  136. CloseableHttpClient httpClient = SimpleHttpsClient.getInstance();
  137. HttpPost httpPost = new HttpPost(url);
  138. try {
  139. List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
  140. if (parameterMap != null) {
  141. for (Entry<String, Object> entry : parameterMap.entrySet()) {
  142. String name = entry.getKey();
  143. String value = ConvertUtils.convert(entry.getValue());
  144. if (StringUtils.isNotEmpty(name)) {
  145. nameValuePairs.add(new BasicNameValuePair(name, value));
  146. }
  147. }
  148. }
  149. httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
  150. HttpResponse httpResponse = httpClient.execute(httpPost);
  151. HttpEntity httpEntity = httpResponse.getEntity();
  152. result = EntityUtils.toString(httpEntity, "UTF-8");
  153. EntityUtils.consume(httpEntity);
  154. } finally {
  155. httpPost.releaseConnection();
  156. httpClient.close();
  157. }
  158. return result;
  159. }
  160. /**
  161. * POST请求https url
  162. *
  163. * @param url
  164. * URL
  165. * @param json
  166. * 请求参数
  167. * @param headers
  168. * 请求头参数
  169. * @return 返回结果
  170. * @throws IOException
  171. * @throws NoSuchAlgorithmException
  172. * @throws KeyManagementException
  173. * @throws KeyStoreException
  174. */
  175. public static String postForHttps(String url, String json, Map<String, String> headers) throws IOException, NoSuchAlgorithmException,
  176. KeyManagementException, KeyStoreException {
  177. String result = null;
  178. CloseableHttpClient httpClient = SimpleHttpsClient.getInstance();
  179. HttpPost httpPost = new HttpPost(url);
  180. if (headers != null && headers.size() > 0) {
  181. for (Entry<String, String> entry : headers.entrySet()) {
  182. httpPost.setHeader(entry.getKey(), entry.getValue());
  183. }
  184. }
  185. try {
  186. httpPost.setEntity(new StringEntity(json, "UTF-8"));
  187. HttpResponse httpResponse = httpClient.execute(httpPost);
  188. HttpEntity httpEntity = httpResponse.getEntity();
  189. result = EntityUtils.toString(httpEntity, "UTF-8");
  190. EntityUtils.consume(httpEntity);
  191. } finally {
  192. httpPost.releaseConnection();
  193. httpClient.close();
  194. }
  195. return result;
  196. }
  197. /**
  198. * 带附件的方法
  199. * @param url
  200. * @param parameterMap
  201. * @param filePath
  202. * @return
  203. * @throws IOException
  204. * @throws NoSuchAlgorithmException
  205. * @throws KeyManagementException
  206. * @throws KeyStoreException
  207. */
  208. public static String postForHttps(String url, Map<String, Object> parameterMap, String filePath) throws IOException, NoSuchAlgorithmException,
  209. KeyManagementException, KeyStoreException {
  210. File file = new File(filePath);
  211. String result = null;
  212. CloseableHttpClient httpClient = HttpClients.createDefault();
  213. HttpPost httpPost = new HttpPost(url);
  214. MultipartEntityBuilder builder = MultipartEntityBuilder.create();
  215. try {
  216. if (parameterMap != null) {
  217. for (Entry<String, Object> entry : parameterMap.entrySet()) {
  218. String name = entry.getKey();
  219. String value = ConvertUtils.convert(entry.getValue());
  220. if (StringUtils.isNotEmpty(name)) {
  221. builder.addTextBody(name, value);
  222. }
  223. }
  224. }
  225. builder.addBinaryBody(file.getName(), file, ContentType.create("application/zip"), file.getName() + ".zip");
  226. httpPost.setEntity(builder.build());
  227. HttpResponse httpResponse = httpClient.execute(httpPost);
  228. HttpEntity httpEntity = httpResponse.getEntity();
  229. result = EntityUtils.toString(httpEntity, "UTF-8");
  230. EntityUtils.consume(httpEntity);
  231. } finally {
  232. httpPost.releaseConnection();
  233. httpClient.close();
  234. }
  235. return result;
  236. }
  237. /**
  238. * post请求
  239. * @param url 请求地址
  240. * @param parameter 参数
  241. * @param headers 头信息
  242. * @param fileMap 附件
  243. * @param contentType 附件的类型
  244. * @return
  245. * @throws IOException
  246. * @throws NoSuchAlgorithmException
  247. * @throws KeyManagementException
  248. * @throws KeyStoreException
  249. */
  250. public static String postForHttps(String url, Map<String, Object> parameterMap, Map<String, String> headers, Map<String, File> fileMap,
  251. ContentType contentType) throws IOException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
  252. String result = null;
  253. CloseableHttpClient httpClient = HttpClients.createDefault();
  254. HttpPost httpPost = new HttpPost(url);
  255. if (headers != null && headers.size() > 0) {
  256. for (Entry<String, String> entry : headers.entrySet()) {
  257. httpPost.setHeader(entry.getKey(), entry.getValue());
  258. }
  259. }
  260. MultipartEntityBuilder builder = MultipartEntityBuilder.create();
  261. try {
  262. if (parameterMap != null) {
  263. for (Entry<String, Object> entry : parameterMap.entrySet()) {
  264. String name = entry.getKey();
  265. String value = ConvertUtils.convert(entry.getValue());
  266. if (StringUtils.isNotBlank(name) && StringUtils.isNotBlank(value)) {
  267. builder.addTextBody(name, value, ContentType.create("text/plain", Charset.forName("UTF-8")));
  268. }
  269. }
  270. }
  271. File file = null;
  272. if (fileMap != null && fileMap.size() > 0) {
  273. for (Entry<String, File> entry : fileMap.entrySet()) {
  274. file = entry.getValue();
  275. if (file != null) {
  276. builder.addBinaryBody(entry.getKey(), file, contentType, file.getName());
  277. }
  278. }
  279. }
  280. httpPost.setEntity(builder.build());
  281. HttpResponse httpResponse = httpClient.execute(httpPost);
  282. HttpEntity httpEntity = httpResponse.getEntity();
  283. result = EntityUtils.toString(httpEntity, "UTF-8");
  284. EntityUtils.consume(httpEntity);
  285. } finally {
  286. httpPost.releaseConnection();
  287. httpClient.close();
  288. }
  289. return result;
  290. }
  291. /**
  292. * GET请求
  293. *
  294. * @param url
  295. * URL
  296. * @param parameterMap
  297. * 请求参数
  298. * @return 返回结果
  299. * @throws IOException
  300. */
  301. public static String get(String url, Map<String, Object> parameterMap) throws IOException {
  302. return get(url, parameterMap, null);
  303. }
  304. /**
  305. * GET请求
  306. *
  307. * @param url
  308. * URL
  309. * @param parameterMap
  310. * 请求参数
  311. * @param headers
  312. * 请求头
  313. * @return 返回结果
  314. * @throws IOException
  315. */
  316. public static String get(String url, Map<String, Object> parameterMap, Map<String, String> headers) throws IOException {
  317. String result = null;
  318. CloseableHttpClient httpClient = HttpClients.createDefault();
  319. HttpGet httpGet = null;
  320. try {
  321. List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
  322. if (parameterMap != null) {
  323. for (Entry<String, Object> entry : parameterMap.entrySet()) {
  324. String name = entry.getKey();
  325. String value = ConvertUtils.convert(entry.getValue());
  326. if (StringUtils.isNotEmpty(name)) {
  327. nameValuePairs.add(new BasicNameValuePair(name, value));
  328. }
  329. }
  330. }
  331. httpGet = new HttpGet(url + (StringUtils.contains(url, "?") ? "&" : "?") + EntityUtils.toString(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")));
  332. if (headers != null && headers.size() > 0) {
  333. for (Entry<String, String> entry : headers.entrySet()) {
  334. httpGet.setHeader(entry.getKey(), entry.getValue());
  335. }
  336. }
  337. HttpResponse httpResponse = httpClient.execute(httpGet);
  338. HttpEntity httpEntity = httpResponse.getEntity();
  339. result = EntityUtils.toString(httpEntity, "UTF-8");
  340. EntityUtils.consume(httpEntity);
  341. } finally {
  342. if (httpGet != null) {
  343. httpGet.releaseConnection();
  344. }
  345. httpClient.close();
  346. }
  347. return result;
  348. }
  349. /**
  350. * POST请求https url
  351. *
  352. * @param url
  353. * URL
  354. * @param parameterMap
  355. * 请求参数
  356. * @return 返回结果
  357. * @throws IOException
  358. * @throws NoSuchAlgorithmException
  359. * @throws KeyManagementException
  360. * @throws KeyStoreException
  361. */
  362. public static boolean downLoadPostForHttps(String url, Map<String, String> headerMap, Map<String, Object> parameterMap, String filePath)
  363. throws IOException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
  364. CloseableHttpClient httpClient = HttpClients.createDefault();
  365. HttpPost httpPost = new HttpPost(url);
  366. if (headerMap != null) {
  367. for (Entry<String, String> entry : headerMap.entrySet()) {
  368. httpPost.addHeader(entry.getKey(), entry.getValue());
  369. }
  370. }
  371. try {
  372. List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
  373. if (parameterMap != null) {
  374. for (Entry<String, Object> entry : parameterMap.entrySet()) {
  375. String name = entry.getKey();
  376. String value = ConvertUtils.convert(entry.getValue());
  377. if (StringUtils.isNotEmpty(name)) {
  378. nameValuePairs.add(new BasicNameValuePair(name, value));
  379. }
  380. }
  381. }
  382. httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
  383. HttpResponse httpResponse = httpClient.execute(httpPost);
  384. HttpEntity httpEntity = httpResponse.getEntity();
  385. InputStream is = httpEntity.getContent();
  386. File file = new File(filePath);
  387. file.getParentFile().mkdirs();
  388. FileOutputStream fileout = new FileOutputStream(file);
  389. IOUtils.copy(is, fileout);
  390. is.close();
  391. fileout.flush();
  392. fileout.close();
  393. EntityUtils.consume(httpEntity);
  394. } finally {
  395. httpPost.releaseConnection();
  396. httpClient.close();
  397. }
  398. return true;
  399. }
  400. /**
  401. * POST请求https url
  402. *
  403. * @param url
  404. * URL
  405. * @param parameterMap
  406. * 请求参数
  407. * @return 返回结果
  408. * @throws IOException
  409. * @throws NoSuchAlgorithmException
  410. * @throws KeyManagementException
  411. * @throws KeyStoreException
  412. */
  413. public static boolean downLoadZipGetForHttps(String url, Map<String, String> headerMap, Map<String, Object> parameterMap, String filePath)
  414. throws IOException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
  415. CloseableHttpClient httpClient = HttpClients.createDefault();
  416. HttpGet httpGet = null;
  417. try {
  418. List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
  419. if (parameterMap != null) {
  420. for (Entry<String, Object> entry : parameterMap.entrySet()) {
  421. String name = entry.getKey();
  422. String value = ConvertUtils.convert(entry.getValue());
  423. if (StringUtils.isNotEmpty(name)) {
  424. nameValuePairs.add(new BasicNameValuePair(name, value));
  425. }
  426. }
  427. }
  428. httpGet = new HttpGet(url + (StringUtils.contains(url, "?") ? "&" : "?") + EntityUtils.toString(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")));
  429. if (headerMap != null) {
  430. for (Entry<String, String> entry : headerMap.entrySet()) {
  431. httpGet.addHeader(entry.getKey(), entry.getValue());
  432. }
  433. }
  434. HttpResponse httpResponse = httpClient.execute(httpGet);
  435. HttpEntity httpEntity = httpResponse.getEntity();
  436. InputStream is = httpEntity.getContent();
  437. File file = new File(filePath);
  438. file.getParentFile().mkdirs();
  439. List<File> files = ZipUtil.unCompressZipFile(is, file.getParent());
  440. List<String> listFile = new ArrayList<String>();
  441. for (File f : files) {
  442. listFile.add(f.getPath());
  443. }
  444. ZipUtil zipUtil = ZipUtil.getInstance();
  445. zipUtil.zipMultiFile(listFile.toArray(new String[listFile.size()]), filePath);
  446. EntityUtils.consume(httpEntity);
  447. for (File f : files) {
  448. FileUtils.deleteQuietly(f);
  449. }
  450. } finally {
  451. if (httpGet != null) {
  452. httpGet.releaseConnection();
  453. }
  454. httpClient.close();
  455. }
  456. return true;
  457. }
  458. }