SmartHttpUtil.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package net.lab1024.smartadmin.util;
  2. import java.nio.charset.Charset;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Map.Entry;
  7. import org.apache.commons.collections.CollectionUtils;
  8. import org.apache.commons.lang3.StringUtils;
  9. import org.apache.http.HttpResponse;
  10. import org.apache.http.HttpStatus;
  11. import org.apache.http.NameValuePair;
  12. import org.apache.http.client.entity.UrlEncodedFormEntity;
  13. import org.apache.http.client.methods.CloseableHttpResponse;
  14. import org.apache.http.client.methods.HttpGet;
  15. import org.apache.http.client.methods.HttpPost;
  16. import org.apache.http.entity.StringEntity;
  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. /**
  22. * [ HttpUtils ]
  23. *
  24. * @author yandanyang
  25. * @version 1.0
  26. * @company 1024lab.net
  27. * @copyright (c) 2019 1024lab.netInc. All rights reserved.
  28. * @date
  29. * @since JDK1.8
  30. */
  31. public class SmartHttpUtil {
  32. public static String sendGet(String url, Map<String, Object> params, Map<String, String> header) throws Exception {
  33. HttpGet httpGet = null;
  34. String body = "";
  35. CloseableHttpClient httpClient = null;
  36. CloseableHttpResponse response = null;
  37. try {
  38. httpClient = HttpClients.createDefault();
  39. List<String> mapList = new ArrayList<>();
  40. if (params != null) {
  41. for (Entry<String, Object> entry : params.entrySet()) {
  42. mapList.add(entry.getKey() + "=" + entry.getValue());
  43. }
  44. }
  45. if (CollectionUtils.isNotEmpty(mapList)) {
  46. url = url + "?";
  47. String paramsStr = StringUtils.join(mapList, "&");
  48. url = url + paramsStr;
  49. }
  50. httpGet = new HttpGet(url);
  51. httpGet.setHeader("Content-type", "application/json; charset=utf-8");
  52. httpGet.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
  53. if (header != null) {
  54. for (Entry<String, String> entry : header.entrySet()) {
  55. httpGet.setHeader(entry.getKey(), entry.getValue());
  56. }
  57. }
  58. response = httpClient.execute(httpGet);
  59. int statusCode = response.getStatusLine().getStatusCode();
  60. if (statusCode != HttpStatus.SC_OK) {
  61. throw new RuntimeException("请求失败");
  62. } else {
  63. body = EntityUtils.toString(response.getEntity(), "UTF-8");
  64. }
  65. } catch (Exception e) {
  66. throw e;
  67. } finally {
  68. if (httpGet != null) {
  69. httpGet.releaseConnection();
  70. }
  71. if (response != null) {
  72. response.close();
  73. }
  74. }
  75. return body;
  76. }
  77. public static String sendPostJson(String url, String json, Map<String, String> header) throws Exception {
  78. HttpPost httpPost = null;
  79. String body = "";
  80. try {
  81. CloseableHttpClient httpClient = HttpClients.createDefault();
  82. httpPost = new HttpPost(url);
  83. httpPost.setHeader("Content-type", "application/json; charset=utf-8");
  84. httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
  85. if (header != null) {
  86. for (Entry<String, String> entry : header.entrySet()) {
  87. httpPost.setHeader(entry.getKey(), entry.getValue());
  88. }
  89. }
  90. StringEntity entity = new StringEntity(json, Charset.forName("UTF-8"));
  91. entity.setContentEncoding("UTF-8");
  92. entity.setContentType("application/json");
  93. httpPost.setEntity(entity);
  94. HttpResponse response = httpClient.execute(httpPost);
  95. int statusCode = response.getStatusLine().getStatusCode();
  96. if (statusCode != HttpStatus.SC_OK) {
  97. throw new RuntimeException("请求失败");
  98. } else {
  99. body = EntityUtils.toString(response.getEntity(), "UTF-8");
  100. }
  101. } catch (Exception e) {
  102. throw e;
  103. } finally {
  104. if (httpPost != null) {
  105. httpPost.releaseConnection();
  106. }
  107. }
  108. return body;
  109. }
  110. public static String sendPostForm(String url, Map<String, String> params, Map<String, String> header) throws Exception {
  111. HttpPost httpPost = null;
  112. String body = "";
  113. try {
  114. CloseableHttpClient httpClient = HttpClients.createDefault();
  115. httpPost = new HttpPost(url);
  116. httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
  117. httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
  118. if (header != null) {
  119. for (Entry<String, String> entry : header.entrySet()) {
  120. httpPost.setHeader(entry.getKey(), entry.getValue());
  121. }
  122. }
  123. List<NameValuePair> nvps = new ArrayList<>();
  124. if (params != null) {
  125. for (Entry<String, String> entry : params.entrySet()) {
  126. nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
  127. }
  128. }
  129. //设置参数到请求对象中
  130. httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
  131. HttpResponse response = httpClient.execute(httpPost);
  132. int statusCode = response.getStatusLine().getStatusCode();
  133. if (statusCode != HttpStatus.SC_OK) {
  134. throw new RuntimeException("请求失败");
  135. } else {
  136. body = EntityUtils.toString(response.getEntity(), "UTF-8");
  137. }
  138. } catch (Exception e) {
  139. throw e;
  140. } finally {
  141. if (httpPost != null) {
  142. httpPost.releaseConnection();
  143. }
  144. }
  145. return body;
  146. }
  147. }