HttpUtil.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. package com.kmall.common.utils;
  2. import com.alibaba.fastjson.JSON;
  3. import org.apache.commons.httpclient.*;
  4. import org.apache.commons.httpclient.cookie.CookiePolicy;
  5. import org.apache.commons.httpclient.cookie.CookieSpec;
  6. import org.apache.commons.httpclient.methods.GetMethod;
  7. import org.apache.commons.httpclient.methods.PostMethod;
  8. import org.apache.commons.httpclient.methods.RequestEntity;
  9. import org.apache.commons.httpclient.methods.StringRequestEntity;
  10. import org.apache.commons.logging.Log;
  11. import org.apache.commons.logging.LogFactory;
  12. import org.apache.http.client.config.RequestConfig;
  13. import org.apache.http.client.methods.CloseableHttpResponse;
  14. import org.apache.http.client.methods.HttpPost;
  15. import org.apache.http.entity.ContentType;
  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.util.EntityUtils;
  20. import org.apache.log4j.Logger;
  21. import java.io.IOException;
  22. import java.io.UnsupportedEncodingException;
  23. import java.net.MalformedURLException;
  24. import java.net.URLEncoder;
  25. import java.nio.charset.Charset;
  26. import java.util.Iterator;
  27. import java.util.Map;
  28. import java.util.Set;
  29. /**
  30. * Http请求通用工具 <br>
  31. *
  32. * @author Scott
  33. * @date 2017年11月18日 下午13:13:23
  34. */
  35. public class HttpUtil {
  36. private static final Log logger = LogFactory.getLog(HttpUtil.class);
  37. /**
  38. * 定义编码格式 UTF-8
  39. */
  40. public static final String URL_PARAM_DECODECHARSET_UTF8 = "UTF-8";
  41. /**
  42. * 定义编码格式 GBK
  43. */
  44. public static final String URL_PARAM_DECODECHARSET_GBK = "GBK";
  45. private static final String URL_PARAM_CONNECT_FLAG = "&";
  46. private static final String EMPTY = "";
  47. private static MultiThreadedHttpConnectionManager connectionManager = null;
  48. private static int connectionTimeOut = 25000;
  49. private static int socketTimeOut = 25000;
  50. private static int maxConnectionPerHost = 20;
  51. private static int maxTotalConnections = 20;
  52. private static HttpClient client;
  53. /**
  54. * 当前登录的人
  55. */
  56. static Cookie[] cookies = null;
  57. // 登录URL
  58. static String loginURL = "/admin/login.do";
  59. // 登录账户
  60. static String loginUserName = "admin";
  61. static String loginPassword = "admin";
  62. static {
  63. connectionManager = new MultiThreadedHttpConnectionManager();
  64. connectionManager.getParams().setConnectionTimeout(connectionTimeOut);
  65. connectionManager.getParams().setSoTimeout(socketTimeOut);
  66. connectionManager.getParams().setDefaultMaxConnectionsPerHost(maxConnectionPerHost);
  67. connectionManager.getParams().setMaxTotalConnections(maxTotalConnections);
  68. client = new HttpClient(connectionManager);
  69. }
  70. /**
  71. * POST方式提交数据
  72. *
  73. * @param url 待请求的URL
  74. * @param params 要提交的数据
  75. * @param enc 编码
  76. * @return 响应结果
  77. * @throws IOException IO异常
  78. */
  79. public static String URLPost(String url, Map<String, Object> params, String enc) {
  80. String response = EMPTY;
  81. PostMethod postMethod = null;
  82. try {
  83. postMethod = new PostMethod(url);
  84. postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=" + enc);
  85. //将表单的值放入postMethod中
  86. Set<String> keySet = params.keySet();
  87. for (String key : keySet) {
  88. Object value = params.get(key);
  89. postMethod.addParameter(key, String.valueOf(value));
  90. }
  91. //执行postMethod
  92. int statusCode = client.executeMethod(postMethod);
  93. if (statusCode == HttpStatus.SC_OK) {
  94. response = postMethod.getResponseBodyAsString();
  95. } else {
  96. logger.error("响应状态码 = " + postMethod.getStatusCode());
  97. }
  98. } catch (HttpException e) {
  99. logger.error("发生致命的异常,可能是协议不对或者返回的内容有问题", e);
  100. e.printStackTrace();
  101. } catch (IOException e) {
  102. logger.error("发生网络异常", e);
  103. e.printStackTrace();
  104. } finally {
  105. if (postMethod != null) {
  106. postMethod.releaseConnection();
  107. postMethod = null;
  108. }
  109. }
  110. return response;
  111. }
  112. /**
  113. * POST方式提交数据
  114. *
  115. * @param url 待请求的URL
  116. * @param params 要提交的数据
  117. * @param enc 编码
  118. * @return 响应结果
  119. * @throws IOException IO异常
  120. */
  121. public static String URLPost(String url, Map<String, Object> header, Map<String, Object> params, String enc) {
  122. String response = EMPTY;
  123. PostMethod postMethod = null;
  124. try {
  125. postMethod = new PostMethod(url);
  126. postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=" + enc);
  127. if (null != header && header.size() > 0) {
  128. for (Map.Entry<String, Object> entry : header.entrySet()) {
  129. postMethod.setRequestHeader(entry.getKey(), String.valueOf(entry.getValue()));
  130. }
  131. }
  132. //将表单的值放入postMethod中
  133. Set<String> keySet = params.keySet();
  134. for (String key : keySet) {
  135. Object value = params.get(key);
  136. postMethod.addParameter(key, String.valueOf(value));
  137. }
  138. //执行postMethod
  139. int statusCode = client.executeMethod(postMethod);
  140. if (statusCode == HttpStatus.SC_OK) {
  141. response = postMethod.getResponseBodyAsString();
  142. } else {
  143. logger.error("响应状态码 = " + postMethod.getStatusCode());
  144. }
  145. } catch (HttpException e) {
  146. logger.error("发生致命的异常,可能是协议不对或者返回的内容有问题", e);
  147. e.printStackTrace();
  148. } catch (IOException e) {
  149. logger.error("发生网络异常", e);
  150. e.printStackTrace();
  151. } finally {
  152. if (postMethod != null) {
  153. postMethod.releaseConnection();
  154. postMethod = null;
  155. }
  156. }
  157. return response;
  158. }
  159. /**
  160. * POST方式提交数据
  161. *
  162. * @param url 待请求的URL
  163. * @param params 要提交的数据
  164. * @param enc 编码
  165. * @return 响应结果
  166. * @throws IOException IO异常
  167. */
  168. public static String URLPostByJsonData(String url, Map<String, Object> header, Map<String, Object> params, String enc) {
  169. String response = EMPTY;
  170. PostMethod postMethod = null;
  171. try {
  172. postMethod = new PostMethod(url);
  173. postMethod.setRequestHeader("Content-Type", "application/json;charset=" + enc);
  174. if (null != header && header.size() > 0) {
  175. for (Map.Entry<String, Object> entry : header.entrySet()) {
  176. postMethod.setRequestHeader(entry.getKey(), String.valueOf(entry.getValue()));
  177. }
  178. }
  179. RequestEntity entity = new StringRequestEntity(JSON.toJSONString(params), "application/json", "UTF-8");
  180. postMethod.setRequestEntity(entity);
  181. //执行postMethod
  182. int statusCode = client.executeMethod(postMethod);
  183. if (statusCode == HttpStatus.SC_OK) {
  184. response = postMethod.getResponseBodyAsString();
  185. } else {
  186. logger.error("响应状态码 = " + postMethod.getStatusCode());
  187. }
  188. } catch (HttpException e) {
  189. logger.error("发生致命的异常,可能是协议不对或者返回的内容有问题", e);
  190. e.printStackTrace();
  191. } catch (IOException e) {
  192. logger.error("发生网络异常", e);
  193. e.printStackTrace();
  194. } finally {
  195. if (postMethod != null) {
  196. postMethod.releaseConnection();
  197. postMethod = null;
  198. }
  199. }
  200. return response;
  201. }
  202. /**
  203. * GET方式提交数据
  204. *
  205. * @param url 待请求的URL
  206. * @param params 要提交的数据
  207. * @param enc 编码
  208. * @return 响应结果
  209. * @throws IOException IO异常
  210. */
  211. public static String URLGet(String url, Map<String, Object> params, String enc) {
  212. String response = EMPTY;
  213. GetMethod getMethod = null;
  214. StringBuffer strtTotalURL = new StringBuffer(EMPTY);
  215. if (strtTotalURL.indexOf("?") == -1) {
  216. strtTotalURL.append(url).append("?").append(getUrl(params, enc));
  217. } else {
  218. strtTotalURL.append(url).append("&").append(getUrl(params, enc));
  219. }
  220. logger.debug("GET请求URL = \n" + strtTotalURL.toString());
  221. try {
  222. getMethod = new GetMethod(strtTotalURL.toString());
  223. getMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=" + enc);
  224. //执行getMethod
  225. int statusCode = client.executeMethod(getMethod);
  226. if (statusCode == HttpStatus.SC_OK) {
  227. response = getMethod.getResponseBodyAsString();
  228. } else {
  229. logger.debug("响应状态码 = " + getMethod.getStatusCode());
  230. }
  231. } catch (HttpException e) {
  232. logger.error("发生致命的异常,可能是协议不对或者返回的内容有问题", e);
  233. e.printStackTrace();
  234. } catch (IOException e) {
  235. logger.error("发生网络异常", e);
  236. e.printStackTrace();
  237. } finally {
  238. if (getMethod != null) {
  239. getMethod.releaseConnection();
  240. getMethod = null;
  241. }
  242. }
  243. return response;
  244. }
  245. /**
  246. * GET方式提交数据
  247. *
  248. * @param url 待请求的URL
  249. * @param params 要提交的数据
  250. * @param enc 编码
  251. * @return 响应结果
  252. * @throws IOException IO异常
  253. */
  254. public static String URLGet(String url, Map<String, Object> header, Map<String, Object> params, String enc) {
  255. String response = EMPTY;
  256. GetMethod getMethod = null;
  257. StringBuffer strtTotalURL = new StringBuffer(EMPTY);
  258. if (strtTotalURL.indexOf("?") == -1) {
  259. strtTotalURL.append(url).append("?").append(getUrl(params, enc));
  260. } else {
  261. strtTotalURL.append(url).append("&").append(getUrl(params, enc));
  262. }
  263. logger.debug("GET请求URL = \n" + strtTotalURL.toString());
  264. try {
  265. getMethod = new GetMethod(strtTotalURL.toString());
  266. getMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=" + enc);
  267. if (null != header && header.size() > 0) {
  268. for (Map.Entry<String, Object> entry : header.entrySet()) {
  269. getMethod.setRequestHeader(entry.getKey(), String.valueOf(entry.getValue()));
  270. }
  271. }
  272. //执行getMethod
  273. int statusCode = client.executeMethod(getMethod);
  274. if (statusCode == HttpStatus.SC_OK) {
  275. response = getMethod.getResponseBodyAsString();
  276. } else {
  277. logger.debug("响应状态码 = " + getMethod.getStatusCode());
  278. }
  279. } catch (HttpException e) {
  280. logger.error("发生致命的异常,可能是协议不对或者返回的内容有问题", e);
  281. e.printStackTrace();
  282. } catch (IOException e) {
  283. logger.error("发生网络异常", e);
  284. e.printStackTrace();
  285. } finally {
  286. if (getMethod != null) {
  287. getMethod.releaseConnection();
  288. getMethod = null;
  289. }
  290. }
  291. return response;
  292. }
  293. /**
  294. * 据Map生成URL字符串
  295. *
  296. * @param map Map
  297. * @param valueEnc URL编码
  298. * @return URL
  299. */
  300. private static String getUrl(Map<String, Object> map, String valueEnc) {
  301. if (null == map || map.keySet().size() == 0) {
  302. return (EMPTY);
  303. }
  304. StringBuffer url = new StringBuffer();
  305. Set<String> keys = map.keySet();
  306. for (Iterator<String> it = keys.iterator(); it.hasNext(); ) {
  307. String key = it.next();
  308. if (map.containsKey(key)) {
  309. String val = map.get(key).toString();
  310. String str = val != null ? val : EMPTY;
  311. try {
  312. str = URLEncoder.encode(str, valueEnc);
  313. } catch (UnsupportedEncodingException e) {
  314. e.printStackTrace();
  315. }
  316. url.append(key).append("=").append(str).append(URL_PARAM_CONNECT_FLAG);
  317. }
  318. }
  319. String strURL = EMPTY;
  320. strURL = url.toString();
  321. if (URL_PARAM_CONNECT_FLAG.equals(EMPTY + strURL.charAt(strURL.length() - 1))) {
  322. strURL = strURL.substring(0, strURL.length() - 1);
  323. }
  324. return (strURL);
  325. }
  326. /**
  327. * POST方式提交数据
  328. *
  329. * @param url 待请求的URL
  330. * @param params 要提交的数据
  331. * @param enc 编码
  332. * @param session 带session
  333. * @return 响应结果
  334. * @throws IOException IO异常
  335. */
  336. public static String URLPost(String url, Map<String, Object> params, String enc, boolean session) {
  337. String response = EMPTY;
  338. PostMethod postMethod = null;
  339. if (!session) {
  340. return URLPost(url, params, enc);
  341. }
  342. try {
  343. postMethod = new PostMethod(url);
  344. postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=" + enc);
  345. //将表单的值放入postMethod中
  346. Set<String> keySet = params.keySet();
  347. for (String key : keySet) {
  348. Object value = params.get(key);
  349. postMethod.addParameter(key, String.valueOf(value));
  350. }
  351. if (null != cookies) {
  352. client.getState().addCookies(cookies);
  353. } else {
  354. getAuthCookie(url, enc);
  355. client.getState().addCookies(cookies);
  356. }
  357. //执行postMethod
  358. int statusCode = client.executeMethod(postMethod);
  359. if (statusCode == HttpStatus.SC_OK) {
  360. response = postMethod.getResponseBodyAsString();
  361. } else {
  362. logger.error("响应状态码 = " + postMethod.getStatusCode());
  363. }
  364. } catch (HttpException e) {
  365. logger.error("发生致命的异常,可能是协议不对或者返回的内容有问题", e);
  366. e.printStackTrace();
  367. } catch (IOException e) {
  368. logger.error("发生网络异常", e);
  369. e.printStackTrace();
  370. } finally {
  371. if (postMethod != null) {
  372. postMethod.releaseConnection();
  373. postMethod = null;
  374. }
  375. }
  376. return response;
  377. }
  378. /**
  379. * 获取session
  380. *
  381. * @param url 待请求的URL
  382. * @param enc 编码
  383. * @return 响应结果
  384. * @throws IOException IO异常
  385. */
  386. public static void getAuthCookie(String url, String enc) {
  387. PostMethod postMethod = null;
  388. try {
  389. Object[] ipPort = getIpPortFormURL(url);
  390. String ip = (String) ipPort[0];
  391. int port = (Integer) ipPort[1];
  392. String logUrl = "http://" + ip + ":" + port + loginURL;
  393. postMethod = new PostMethod(logUrl);
  394. postMethod.addParameter("username", loginUserName);
  395. postMethod.addParameter("password", loginPassword);
  396. postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=" + enc);
  397. //执行postMethod
  398. int statusCode = client.executeMethod(postMethod);
  399. // 查看 cookie 信息
  400. CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
  401. cookies = cookiespec.match(ip, port, "/", false, client.getState().getCookies());
  402. logger.error("响应状态码 = " + postMethod.getStatusCode());
  403. } catch (HttpException e) {
  404. logger.error("发生致命的异常,可能是协议不对或者返回的内容有问题", e);
  405. e.printStackTrace();
  406. } catch (IOException e) {
  407. logger.error("发生网络异常", e);
  408. e.printStackTrace();
  409. } finally {
  410. if (postMethod != null) {
  411. postMethod.releaseConnection();
  412. postMethod = null;
  413. }
  414. }
  415. }
  416. /**
  417. * 解析URL的端口和ip
  418. *
  419. * @param URL
  420. * @return
  421. */
  422. public static Object[] getIpPortFormURL(String URL) {
  423. Object[] ip_port = new Object[2];
  424. try {
  425. java.net.URL url = new java.net.URL(URL);
  426. ip_port[0] = url.getHost();
  427. ip_port[1] = url.getPort() != -1 ? url.getPort() : 80;
  428. } catch (MalformedURLException e) {
  429. e.printStackTrace();
  430. }
  431. return ip_port;
  432. }
  433. public static void setLoginPassword(String loginPassword) {
  434. HttpUtil.loginPassword = loginPassword;
  435. }
  436. public static void setLoginUserName(String loginUserName) {
  437. HttpUtil.loginUserName = loginUserName;
  438. }
  439. public static void setLoginURL(String loginURL) {
  440. HttpUtil.loginURL = loginURL;
  441. }
  442. public static void main(String[] args) throws MalformedURLException {
  443. java.net.URL url = new java.net.URL("http://blog.csdn.net/zhujianlin1990");
  444. System.out.println(url.getHost());
  445. System.out.println(url.getPort());
  446. }
  447. /**
  448. * @method
  449. * @description 发送post请求,json参数
  450. * @date: 2019/9/10 13:58
  451. * @author: 叶静
  452. * @Param
  453. * @return
  454. */
  455. public static String doPostJson(String url, String json) {
  456. // 创建Httpclient对象
  457. CloseableHttpClient httpClient = HttpClients.createDefault();
  458. CloseableHttpResponse response = null;
  459. String resultString = "";
  460. try {
  461. // 创建Http Post请求
  462. HttpPost httpPost = new HttpPost(url);
  463. RequestConfig config = RequestConfig.custom()
  464. .setConnectTimeout(10000)// 10秒 连接主机的超时时间(单位:毫秒)
  465. .setSocketTimeout(10000).build(); // 10秒 从主机读取数据的超时时间(单位:毫秒)
  466. httpPost.setConfig(config);
  467. httpPost.setEntity(new StringEntity(json, Charset.forName("UTF-8")));
  468. // 创建请求内容
  469. StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
  470. httpPost.setEntity(entity);
  471. // 执行http请求
  472. response = httpClient.execute(httpPost);
  473. resultString = EntityUtils.toString(response.getEntity(), "utf-8");
  474. } catch (Exception e) {
  475. logger.error("调用HttpClientUtil.doPostJson, url=" + url + ",param=" + json, e);
  476. } finally {
  477. try {
  478. response.close();
  479. } catch (IOException e) {
  480. logger.error("调用HttpClientUtil.doPostJson, url=" + url + ",param=" + json, e);
  481. }
  482. }
  483. return resultString;
  484. }
  485. }