敏捷工具
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

322 lines
10 KiB

2 years ago
  1. package com.wudao.mall.mall2.util;
  2. import java.io.UnsupportedEncodingException;
  3. import java.net.URLEncoder;
  4. import java.security.KeyManagementException;
  5. import java.security.NoSuchAlgorithmException;
  6. import java.security.cert.X509Certificate;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.Map;
  10. import javax.net.ssl.SSLContext;
  11. import javax.net.ssl.TrustManager;
  12. import javax.net.ssl.X509TrustManager;
  13. import org.apache.commons.lang.StringUtils;
  14. import org.apache.http.HttpResponse;
  15. import org.apache.http.NameValuePair;
  16. import org.apache.http.client.HttpClient;
  17. import org.apache.http.client.entity.UrlEncodedFormEntity;
  18. import org.apache.http.client.methods.HttpDelete;
  19. import org.apache.http.client.methods.HttpGet;
  20. import org.apache.http.client.methods.HttpPost;
  21. import org.apache.http.client.methods.HttpPut;
  22. import org.apache.http.conn.ClientConnectionManager;
  23. import org.apache.http.conn.scheme.Scheme;
  24. import org.apache.http.conn.scheme.SchemeRegistry;
  25. import org.apache.http.conn.ssl.SSLSocketFactory;
  26. import org.apache.http.entity.ByteArrayEntity;
  27. import org.apache.http.entity.StringEntity;
  28. import org.apache.http.impl.client.DefaultHttpClient;
  29. import org.apache.http.message.BasicNameValuePair;
  30. import org.springframework.beans.factory.annotation.Autowired;
  31. /**
  32. * 功能描述:
  33. *
  34. * @author Administrator
  35. * @date 2021/7/23
  36. */
  37. public class HttpUtils {
  38. /**
  39. * get
  40. *
  41. * @param host
  42. * @param path
  43. * @param method
  44. * @param headers
  45. * @param querys
  46. * @return
  47. * @throws Exception
  48. */
  49. public static HttpResponse doGet(String host, String path, String method,
  50. Map<String, String> headers,
  51. Map<String, String> querys)
  52. throws Exception {
  53. HttpClient httpClient = wrapClient(host);
  54. HttpGet request = new HttpGet(buildUrl(host, path, querys));
  55. for (Map.Entry<String, String> e : headers.entrySet()) {
  56. request.addHeader(e.getKey(), e.getValue());
  57. }
  58. return httpClient.execute(request);
  59. }
  60. /**
  61. * post form
  62. *
  63. * @param host
  64. * @param path
  65. * @param method
  66. * @param headers
  67. * @param querys
  68. * @param bodys
  69. * @return
  70. * @throws Exception
  71. */
  72. public static HttpResponse doPost(String host, String path, String method,
  73. Map<String, String> headers,
  74. Map<String, String> querys,
  75. Map<String, String> bodys)
  76. throws Exception {
  77. HttpClient httpClient = wrapClient(host);
  78. HttpPost request = new HttpPost(buildUrl(host, path, querys));
  79. for (Map.Entry<String, String> e : headers.entrySet()) {
  80. request.addHeader(e.getKey(), e.getValue());
  81. }
  82. if (bodys != null) {
  83. List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
  84. for (String key : bodys.keySet()) {
  85. nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));
  86. }
  87. UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");
  88. formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
  89. request.setEntity(formEntity);
  90. }
  91. return httpClient.execute(request);
  92. }
  93. /**
  94. * Post String
  95. *
  96. * @param host
  97. * @param path
  98. * @param method
  99. * @param headers
  100. * @param querys
  101. * @param body
  102. * @return
  103. * @throws Exception
  104. */
  105. public static HttpResponse doPost(String host, String path, String method,
  106. Map<String, String> headers,
  107. Map<String, String> querys,
  108. String body)
  109. throws Exception {
  110. HttpClient httpClient = wrapClient(host);
  111. HttpPost request = new HttpPost(buildUrl(host, path, querys));
  112. for (Map.Entry<String, String> e : headers.entrySet()) {
  113. request.addHeader(e.getKey(), e.getValue());
  114. }
  115. if (StringUtils.isNotBlank(body)) {
  116. request.setEntity(new StringEntity(body, "utf-8"));
  117. }
  118. return httpClient.execute(request);
  119. }
  120. /**
  121. * Post stream
  122. *
  123. * @param host
  124. * @param path
  125. * @param method
  126. * @param headers
  127. * @param querys
  128. * @param body
  129. * @return
  130. * @throws Exception
  131. */
  132. public static HttpResponse doPost(String host, String path, String method,
  133. Map<String, String> headers,
  134. Map<String, String> querys,
  135. byte[] body)
  136. throws Exception {
  137. HttpClient httpClient = wrapClient(host);
  138. HttpPost request = new HttpPost(buildUrl(host, path, querys));
  139. for (Map.Entry<String, String> e : headers.entrySet()) {
  140. request.addHeader(e.getKey(), e.getValue());
  141. }
  142. if (body != null) {
  143. request.setEntity(new ByteArrayEntity(body));
  144. }
  145. return httpClient.execute(request);
  146. }
  147. /**
  148. * Put String
  149. * @param host
  150. * @param path
  151. * @param method
  152. * @param headers
  153. * @param querys
  154. * @param body
  155. * @return
  156. * @throws Exception
  157. */
  158. public static HttpResponse doPut(String host, String path, String method,
  159. Map<String, String> headers,
  160. Map<String, String> querys,
  161. String body)
  162. throws Exception {
  163. HttpClient httpClient = wrapClient(host);
  164. HttpPut request = new HttpPut(buildUrl(host, path, querys));
  165. for (Map.Entry<String, String> e : headers.entrySet()) {
  166. request.addHeader(e.getKey(), e.getValue());
  167. }
  168. if (StringUtils.isNotBlank(body)) {
  169. request.setEntity(new StringEntity(body, "utf-8"));
  170. }
  171. return httpClient.execute(request);
  172. }
  173. /**
  174. * Put stream
  175. * @param host
  176. * @param path
  177. * @param method
  178. * @param headers
  179. * @param querys
  180. * @param body
  181. * @return
  182. * @throws Exception
  183. */
  184. public static HttpResponse doPut(String host, String path, String method,
  185. Map<String, String> headers,
  186. Map<String, String> querys,
  187. byte[] body)
  188. throws Exception {
  189. HttpClient httpClient = wrapClient(host);
  190. HttpPut request = new HttpPut(buildUrl(host, path, querys));
  191. for (Map.Entry<String, String> e : headers.entrySet()) {
  192. request.addHeader(e.getKey(), e.getValue());
  193. }
  194. if (body != null) {
  195. request.setEntity(new ByteArrayEntity(body));
  196. }
  197. return httpClient.execute(request);
  198. }
  199. /**
  200. * Delete
  201. *
  202. * @param host
  203. * @param path
  204. * @param method
  205. * @param headers
  206. * @param querys
  207. * @return
  208. * @throws Exception
  209. */
  210. public static HttpResponse doDelete(String host, String path, String method,
  211. Map<String, String> headers,
  212. Map<String, String> querys)
  213. throws Exception {
  214. HttpClient httpClient = wrapClient(host);
  215. HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
  216. for (Map.Entry<String, String> e : headers.entrySet()) {
  217. request.addHeader(e.getKey(), e.getValue());
  218. }
  219. return httpClient.execute(request);
  220. }
  221. private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException {
  222. StringBuilder sbUrl = new StringBuilder();
  223. sbUrl.append(host);
  224. if (!StringUtils.isBlank(path)) {
  225. sbUrl.append(path);
  226. }
  227. if (null != querys) {
  228. StringBuilder sbQuery = new StringBuilder();
  229. for (Map.Entry<String, String> query : querys.entrySet()) {
  230. if (0 < sbQuery.length()) {
  231. sbQuery.append("&");
  232. }
  233. if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
  234. sbQuery.append(query.getValue());
  235. }
  236. if (!StringUtils.isBlank(query.getKey())) {
  237. sbQuery.append(query.getKey());
  238. if (!StringUtils.isBlank(query.getValue())) {
  239. sbQuery.append("=");
  240. sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
  241. }
  242. }
  243. }
  244. if (0 < sbQuery.length()) {
  245. sbUrl.append("?").append(sbQuery);
  246. }
  247. }
  248. return sbUrl.toString();
  249. }
  250. private static HttpClient wrapClient(String host) {
  251. HttpClient httpClient = new DefaultHttpClient();
  252. if (host.startsWith("https://")) {
  253. sslClient(httpClient);
  254. }
  255. return httpClient;
  256. }
  257. private static void sslClient(HttpClient httpClient) {
  258. try {
  259. SSLContext ctx = SSLContext.getInstance("TLS");
  260. X509TrustManager tm = new X509TrustManager() {
  261. @Override
  262. public X509Certificate[] getAcceptedIssuers() {
  263. return null;
  264. }
  265. @Override
  266. public void checkClientTrusted(X509Certificate[] xcs, String str) {
  267. }
  268. @Override
  269. public void checkServerTrusted(X509Certificate[] xcs, String str) {
  270. }
  271. };
  272. ctx.init(null, new TrustManager[] { tm }, null);
  273. SSLSocketFactory ssf = new SSLSocketFactory(ctx);
  274. ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
  275. ClientConnectionManager ccm = httpClient.getConnectionManager();
  276. SchemeRegistry registry = ccm.getSchemeRegistry();
  277. registry.register(new Scheme("https", 443, ssf));
  278. } catch (KeyManagementException ex) {
  279. throw new RuntimeException(ex);
  280. } catch (NoSuchAlgorithmException ex) {
  281. throw new RuntimeException(ex);
  282. }
  283. }
  284. }