123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 |
- package com.kmall.common.utils;
- import com.alibaba.fastjson.JSON;
- import org.apache.commons.httpclient.*;
- import org.apache.commons.httpclient.cookie.CookiePolicy;
- import org.apache.commons.httpclient.cookie.CookieSpec;
- import org.apache.commons.httpclient.methods.GetMethod;
- import org.apache.commons.httpclient.methods.PostMethod;
- import org.apache.commons.httpclient.methods.RequestEntity;
- import org.apache.commons.httpclient.methods.StringRequestEntity;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.apache.log4j.Logger;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import java.net.MalformedURLException;
- import java.net.URLEncoder;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- /**
- * Http请求通用工具 <br>
- *
- * @author Scott
- * @date 2017年11月18日 下午13:13:23
- */
- public class HttpUtil {
- private static final Log logger = LogFactory.getLog(HttpUtil.class);
- /**
- * 定义编码格式 UTF-8
- */
- public static final String URL_PARAM_DECODECHARSET_UTF8 = "UTF-8";
- /**
- * 定义编码格式 GBK
- */
- public static final String URL_PARAM_DECODECHARSET_GBK = "GBK";
- private static final String URL_PARAM_CONNECT_FLAG = "&";
- private static final String EMPTY = "";
- private static MultiThreadedHttpConnectionManager connectionManager = null;
- private static int connectionTimeOut = 25000;
- private static int socketTimeOut = 25000;
- private static int maxConnectionPerHost = 20;
- private static int maxTotalConnections = 20;
- private static HttpClient client;
- /**
- * 当前登录的人
- */
- static Cookie[] cookies = null;
- // 登录URL
- static String loginURL = "/admin/login.do";
- // 登录账户
- static String loginUserName = "admin";
- static String loginPassword = "admin";
- static {
- connectionManager = new MultiThreadedHttpConnectionManager();
- connectionManager.getParams().setConnectionTimeout(connectionTimeOut);
- connectionManager.getParams().setSoTimeout(socketTimeOut);
- connectionManager.getParams().setDefaultMaxConnectionsPerHost(maxConnectionPerHost);
- connectionManager.getParams().setMaxTotalConnections(maxTotalConnections);
- client = new HttpClient(connectionManager);
- }
- /**
- * POST方式提交数据
- *
- * @param url 待请求的URL
- * @param params 要提交的数据
- * @param enc 编码
- * @return 响应结果
- * @throws IOException IO异常
- */
- public static String URLPost(String url, Map<String, Object> params, String enc) {
- String response = EMPTY;
- PostMethod postMethod = null;
- try {
- postMethod = new PostMethod(url);
- postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=" + enc);
- //将表单的值放入postMethod中
- Set<String> keySet = params.keySet();
- for (String key : keySet) {
- Object value = params.get(key);
- postMethod.addParameter(key, String.valueOf(value));
- }
- //执行postMethod
- int statusCode = client.executeMethod(postMethod);
- if (statusCode == HttpStatus.SC_OK) {
- response = postMethod.getResponseBodyAsString();
- } else {
- logger.error("响应状态码 = " + postMethod.getStatusCode());
- }
- } catch (HttpException e) {
- logger.error("发生致命的异常,可能是协议不对或者返回的内容有问题", e);
- e.printStackTrace();
- } catch (IOException e) {
- logger.error("发生网络异常", e);
- e.printStackTrace();
- } finally {
- if (postMethod != null) {
- postMethod.releaseConnection();
- postMethod = null;
- }
- }
- return response;
- }
- /**
- * POST方式提交数据
- *
- * @param url 待请求的URL
- * @param params 要提交的数据
- * @param enc 编码
- * @return 响应结果
- * @throws IOException IO异常
- */
- public static String URLPost(String url, Map<String, Object> header, Map<String, Object> params, String enc) {
- String response = EMPTY;
- PostMethod postMethod = null;
- try {
- postMethod = new PostMethod(url);
- postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=" + enc);
- if (null != header && header.size() > 0) {
- for (Map.Entry<String, Object> entry : header.entrySet()) {
- postMethod.setRequestHeader(entry.getKey(), String.valueOf(entry.getValue()));
- }
- }
- //将表单的值放入postMethod中
- Set<String> keySet = params.keySet();
- for (String key : keySet) {
- Object value = params.get(key);
- postMethod.addParameter(key, String.valueOf(value));
- }
- //执行postMethod
- int statusCode = client.executeMethod(postMethod);
- if (statusCode == HttpStatus.SC_OK) {
- response = postMethod.getResponseBodyAsString();
- } else {
- logger.error("响应状态码 = " + postMethod.getStatusCode());
- }
- } catch (HttpException e) {
- logger.error("发生致命的异常,可能是协议不对或者返回的内容有问题", e);
- e.printStackTrace();
- } catch (IOException e) {
- logger.error("发生网络异常", e);
- e.printStackTrace();
- } finally {
- if (postMethod != null) {
- postMethod.releaseConnection();
- postMethod = null;
- }
- }
- return response;
- }
- /**
- * POST方式提交数据
- *
- * @param url 待请求的URL
- * @param params 要提交的数据
- * @param enc 编码
- * @return 响应结果
- * @throws IOException IO异常
- */
- public static String URLPostByJsonData(String url, Map<String, Object> header, Map<String, Object> params, String enc) {
- String response = EMPTY;
- PostMethod postMethod = null;
- try {
- postMethod = new PostMethod(url);
- postMethod.setRequestHeader("Content-Type", "application/json;charset=" + enc);
- if (null != header && header.size() > 0) {
- for (Map.Entry<String, Object> entry : header.entrySet()) {
- postMethod.setRequestHeader(entry.getKey(), String.valueOf(entry.getValue()));
- }
- }
- RequestEntity entity = new StringRequestEntity(JSON.toJSONString(params), "application/json", "UTF-8");
- postMethod.setRequestEntity(entity);
- //执行postMethod
- int statusCode = client.executeMethod(postMethod);
- if (statusCode == HttpStatus.SC_OK) {
- response = postMethod.getResponseBodyAsString();
- } else {
- logger.error("响应状态码 = " + postMethod.getStatusCode());
- }
- } catch (HttpException e) {
- logger.error("发生致命的异常,可能是协议不对或者返回的内容有问题", e);
- e.printStackTrace();
- } catch (IOException e) {
- logger.error("发生网络异常", e);
- e.printStackTrace();
- } finally {
- if (postMethod != null) {
- postMethod.releaseConnection();
- postMethod = null;
- }
- }
- return response;
- }
- /**
- * GET方式提交数据
- *
- * @param url 待请求的URL
- * @param params 要提交的数据
- * @param enc 编码
- * @return 响应结果
- * @throws IOException IO异常
- */
- public static String URLGet(String url, Map<String, Object> params, String enc) {
- String response = EMPTY;
- GetMethod getMethod = null;
- StringBuffer strtTotalURL = new StringBuffer(EMPTY);
- if (strtTotalURL.indexOf("?") == -1) {
- strtTotalURL.append(url).append("?").append(getUrl(params, enc));
- } else {
- strtTotalURL.append(url).append("&").append(getUrl(params, enc));
- }
- logger.debug("GET请求URL = \n" + strtTotalURL.toString());
- try {
- getMethod = new GetMethod(strtTotalURL.toString());
- getMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=" + enc);
- //执行getMethod
- int statusCode = client.executeMethod(getMethod);
- if (statusCode == HttpStatus.SC_OK) {
- response = getMethod.getResponseBodyAsString();
- } else {
- logger.debug("响应状态码 = " + getMethod.getStatusCode());
- }
- } catch (HttpException e) {
- logger.error("发生致命的异常,可能是协议不对或者返回的内容有问题", e);
- e.printStackTrace();
- } catch (IOException e) {
- logger.error("发生网络异常", e);
- e.printStackTrace();
- } finally {
- if (getMethod != null) {
- getMethod.releaseConnection();
- getMethod = null;
- }
- }
- return response;
- }
- /**
- * GET方式提交数据
- *
- * @param url 待请求的URL
- * @param params 要提交的数据
- * @param enc 编码
- * @return 响应结果
- * @throws IOException IO异常
- */
- public static String URLGet(String url, Map<String, Object> header, Map<String, Object> params, String enc) {
- String response = EMPTY;
- GetMethod getMethod = null;
- StringBuffer strtTotalURL = new StringBuffer(EMPTY);
- if (strtTotalURL.indexOf("?") == -1) {
- strtTotalURL.append(url).append("?").append(getUrl(params, enc));
- } else {
- strtTotalURL.append(url).append("&").append(getUrl(params, enc));
- }
- logger.debug("GET请求URL = \n" + strtTotalURL.toString());
- try {
- getMethod = new GetMethod(strtTotalURL.toString());
- getMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=" + enc);
- if (null != header && header.size() > 0) {
- for (Map.Entry<String, Object> entry : header.entrySet()) {
- getMethod.setRequestHeader(entry.getKey(), String.valueOf(entry.getValue()));
- }
- }
- //执行getMethod
- int statusCode = client.executeMethod(getMethod);
- if (statusCode == HttpStatus.SC_OK) {
- response = getMethod.getResponseBodyAsString();
- } else {
- logger.debug("响应状态码 = " + getMethod.getStatusCode());
- }
- } catch (HttpException e) {
- logger.error("发生致命的异常,可能是协议不对或者返回的内容有问题", e);
- e.printStackTrace();
- } catch (IOException e) {
- logger.error("发生网络异常", e);
- e.printStackTrace();
- } finally {
- if (getMethod != null) {
- getMethod.releaseConnection();
- getMethod = null;
- }
- }
- return response;
- }
- /**
- * 据Map生成URL字符串
- *
- * @param map Map
- * @param valueEnc URL编码
- * @return URL
- */
- private static String getUrl(Map<String, Object> map, String valueEnc) {
- if (null == map || map.keySet().size() == 0) {
- return (EMPTY);
- }
- StringBuffer url = new StringBuffer();
- Set<String> keys = map.keySet();
- for (Iterator<String> it = keys.iterator(); it.hasNext(); ) {
- String key = it.next();
- if (map.containsKey(key)) {
- String val = map.get(key).toString();
- String str = val != null ? val : EMPTY;
- try {
- str = URLEncoder.encode(str, valueEnc);
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- url.append(key).append("=").append(str).append(URL_PARAM_CONNECT_FLAG);
- }
- }
- String strURL = EMPTY;
- strURL = url.toString();
- if (URL_PARAM_CONNECT_FLAG.equals(EMPTY + strURL.charAt(strURL.length() - 1))) {
- strURL = strURL.substring(0, strURL.length() - 1);
- }
- return (strURL);
- }
- /**
- * POST方式提交数据
- *
- * @param url 待请求的URL
- * @param params 要提交的数据
- * @param enc 编码
- * @param session 带session
- * @return 响应结果
- * @throws IOException IO异常
- */
- public static String URLPost(String url, Map<String, Object> params, String enc, boolean session) {
- String response = EMPTY;
- PostMethod postMethod = null;
- if (!session) {
- return URLPost(url, params, enc);
- }
- try {
- postMethod = new PostMethod(url);
- postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=" + enc);
- //将表单的值放入postMethod中
- Set<String> keySet = params.keySet();
- for (String key : keySet) {
- Object value = params.get(key);
- postMethod.addParameter(key, String.valueOf(value));
- }
- if (null != cookies) {
- client.getState().addCookies(cookies);
- } else {
- getAuthCookie(url, enc);
- client.getState().addCookies(cookies);
- }
- //执行postMethod
- int statusCode = client.executeMethod(postMethod);
- if (statusCode == HttpStatus.SC_OK) {
- response = postMethod.getResponseBodyAsString();
- } else {
- logger.error("响应状态码 = " + postMethod.getStatusCode());
- }
- } catch (HttpException e) {
- logger.error("发生致命的异常,可能是协议不对或者返回的内容有问题", e);
- e.printStackTrace();
- } catch (IOException e) {
- logger.error("发生网络异常", e);
- e.printStackTrace();
- } finally {
- if (postMethod != null) {
- postMethod.releaseConnection();
- postMethod = null;
- }
- }
- return response;
- }
- /**
- * 获取session
- *
- * @param url 待请求的URL
- * @param enc 编码
- * @return 响应结果
- * @throws IOException IO异常
- */
- public static void getAuthCookie(String url, String enc) {
- PostMethod postMethod = null;
- try {
- Object[] ipPort = getIpPortFormURL(url);
- String ip = (String) ipPort[0];
- int port = (Integer) ipPort[1];
- String logUrl = "http://" + ip + ":" + port + loginURL;
- postMethod = new PostMethod(logUrl);
- postMethod.addParameter("username", loginUserName);
- postMethod.addParameter("password", loginPassword);
- postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=" + enc);
- //执行postMethod
- int statusCode = client.executeMethod(postMethod);
- // 查看 cookie 信息
- CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
- cookies = cookiespec.match(ip, port, "/", false, client.getState().getCookies());
- logger.error("响应状态码 = " + postMethod.getStatusCode());
- } catch (HttpException e) {
- logger.error("发生致命的异常,可能是协议不对或者返回的内容有问题", e);
- e.printStackTrace();
- } catch (IOException e) {
- logger.error("发生网络异常", e);
- e.printStackTrace();
- } finally {
- if (postMethod != null) {
- postMethod.releaseConnection();
- postMethod = null;
- }
- }
- }
- /**
- * 解析URL的端口和ip
- *
- * @param URL
- * @return
- */
- public static Object[] getIpPortFormURL(String URL) {
- Object[] ip_port = new Object[2];
- try {
- java.net.URL url = new java.net.URL(URL);
- ip_port[0] = url.getHost();
- ip_port[1] = url.getPort() != -1 ? url.getPort() : 80;
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- return ip_port;
- }
- public static void setLoginPassword(String loginPassword) {
- HttpUtil.loginPassword = loginPassword;
- }
- public static void setLoginUserName(String loginUserName) {
- HttpUtil.loginUserName = loginUserName;
- }
- public static void setLoginURL(String loginURL) {
- HttpUtil.loginURL = loginURL;
- }
- public static void main(String[] args) throws MalformedURLException {
- java.net.URL url = new java.net.URL("http://blog.csdn.net/zhujianlin1990");
- System.out.println(url.getHost());
- System.out.println(url.getPort());
- }
- }
|