HttpUtil.java 17 KB

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