敏捷工具
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.

320 lines
10 KiB

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