HttpQuery httpClient with cookie sessionID
2015-03-16 18:50
295 查看
httpClient-4.3.3 实用工具,提供个了cookie功能
maven:
HttpQuery.java
HttpUtil
Good luck!~
maven:
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.3</version> </dependency>
HttpQuery.java
import org.apache.http.Consts; import org.apache.http.Header; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URIBuilder; import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.DefaultHttpRequestRetryHandler; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import org.apache.log4j.Logger; import java.net.URI; import java.net.URL; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class HttpQuery { private static Logger log = Logger.getLogger(HttpQuery.class); private CloseableHttpClient httpClient = null; private HttpClientBuilder builder = null; private HttpGet httpGet = null; private HttpPost httpPost = null; private Header[] headers = null; private String cookies = null; private String referer; private Map<String, String> cookieMap = new HashMap<String, String>(); private Boolean autoClose = true; private String lastError; public HttpQuery(){ this(true); } public HttpQuery(boolean autoClose){ this.autoClose = autoClose; } public void setAutoClose(Boolean autoClose) { this.autoClose = autoClose; } /** * 在HttpClient 4.0+的版本,正确的关闭连接如下: * EntityUtils.consumeQuietly(response.getEntity()); * 在HttpClient 3.0+版本中,正确的关闭如下: * method.releaseConnection(); */ private void init() { if (this.httpClient == null) { builder = HttpClientBuilder.create(); RequestConfig.Builder reqBuild = RequestConfig.copy(RequestConfig.DEFAULT); reqBuild.setConnectTimeout(30 * 1000); reqBuild.setSocketTimeout(30 * 1000); reqBuild.setRedirectsEnabled(true); RequestConfig reqConfig = reqBuild.build(); // builder.setRetryHandler(new DefaultHttpRequestRetryHandler());//默认失败后重发3次 builder.setDefaultRequestConfig(reqConfig); builder.setUserAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36"); this.httpClient = builder.build(); } } public String httpGet(String url, Map<String, Object> parameters) throws Exception { this.init(); URL reqURL = new URL(url); URI urlTemp = reqURL.toURI(); URIBuilder uriBuilder = new URIBuilder().setScheme(urlTemp.getScheme()).setHost(urlTemp.getHost()).setPort(urlTemp.getPort()).setPath(urlTemp.getPath()); String query = reqURL.getQuery(); uriBuilder.setParameters(URLEncodedUtils.parse(query, Consts.UTF_8)); if (parameters != null && parameters.size() > 0) { Iterator it = parameters.keySet().iterator(); while (it.hasNext()) { String key = (String) it.next(); if (key == null) continue; if (parameters.get(key) != null) { uriBuilder.setParameter(key, parameters.get(key).toString()); } else { uriBuilder.setParameter(key, ""); } } } URI uri = uriBuilder.build(); this.httpGet = new HttpGet(uri); RequestConfig.Builder reqBuild = RequestConfig.copy(RequestConfig.DEFAULT); RequestConfig requestConfig = reqBuild.build(); httpGet.setConfig(requestConfig); if (this.headers == null) this.headers = httpGet.getAllHeaders(); httpGet.addHeader("Referer", referer); httpGet.setHeader("Cookie", this.getCookies()); HttpResponse response = httpClient.execute(httpGet); this.referer = url; this.parseCookie(response); this.parseHeaders(response); String html = null; if (response != null) { html = EntityUtils.toString(response.getEntity()); EntityUtils.consumeQuietly(response.getEntity());//在HttpClient 4.0+的版本,正确的关闭连接 } if (this.autoClose) this.close(); return html; } public int getCode(){ return this.respCode; } private Header contentType; private void setContentType(Header contentType){ if(contentType!=null) { this.contentType = contentType; String content = this.contentType.getValue(); Pattern pattern = Pattern.compile("charset\\=(.*)"); Matcher m = pattern.matcher(content); if(m.find()){ this.contentEncoding = m.group(1); } } } public String getContentType(){ if(this.contentType!=null) return this.contentType.getValue(); return ""; } public void close() { if (this.httpClient != null) { try { this.httpClient.close(); } catch (Exception e) { e.printStackTrace(); log.error("httpClient.close error", e); } this.httpClient = null; this.builder = null; } } private void parseCookie(HttpResponse response) { Header[] cookieHeaders = response.getHeaders("Set-Cookie"); if (this.cookies == null) this.cookies = ""; for (Header s : cookieHeaders) { String str = s.toString().substring("Set-Cookie:".length()); String[] temps = str.split(";"); if (temps != null) { for (String temp : temps) { if (temp != null) { int index = temp.indexOf("="); if (index > 0 && temp.length() > (index + 1)) { this.cookieMap.put(temp.substring(0, index), temp.substring(index)); } } } } } } public Map getCookie(){ return this.cookieMap; } private String getCookies() { StringBuffer sb = new StringBuffer(); String[] keys = this.cookieMap.keySet().toArray(new String[0]); for (String key : keys) { sb.append(key); sb.append(this.cookieMap.get(key)); sb.append(";"); } return sb.toString(); } public Header[] getHeaders(){ return this.headers; } public int getRespCode(){ return this.respCode; } private int respCode = 200; private Object parseHeaders(HttpResponse response) throws Exception { this.headers = response.getAllHeaders(); this.respCode = response.getStatusLine().getStatusCode(); this.setContentType(response.getEntity().getContentType()); return null; } public String httpPost(String url) throws Exception { return this.httpPost(url, new HashMap<String, Object>()); } public String httpPost(String url, List<NameValuePair> params) throws Exception { Map<String, Object> map = new HashMap<String, Object>(); for (NameValuePair nameValuePair : params) { map.put(nameValuePair.getName(), nameValuePair.getValue()); } return this.httpPost(url, map); } public String httpPost(String url, Map<String, Object> parameters) throws Exception { this.init(); URL reqURL = new URL(url); URI urlTemp = reqURL.toURI(); URIBuilder uriBuilder = new URIBuilder().setScheme(urlTemp.getScheme()).setHost(urlTemp.getHost()).setPort(urlTemp.getPort()).setPath(urlTemp.getPath()); String query = reqURL.getQuery(); uriBuilder.setParameters(URLEncodedUtils.parse(query, Consts.UTF_8)); URI uri = uriBuilder.build(); this.httpPost = new HttpPost(uri); List<NameValuePair> list = null; if (parameters != null && parameters.size() > 0) { list = new ArrayList<NameValuePair>(); Iterator it = parameters.keySet().iterator(); while (it.hasNext()) { String key = (String) it.next(); if (key == null) continue; if (parameters.get(key) != null) list.add(new BasicNameValuePair(key, parameters.get(key).toString())); else list.add(new BasicNameValuePair(key, "")); } } if (list != null && list.size() > 0) httpPost.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8)); if (this.headers == null) this.headers = httpPost.getAllHeaders(); httpPost.addHeader("Referer", referer); httpPost.setHeader("Cookie", this.getCookies()); HttpResponse response = httpClient.execute(httpPost); this.referer = url; this.parseCookie(response); this.parseHeaders(response); String html = null; if (response != null) { html = EntityUtils.toString(response.getEntity()); EntityUtils.consumeQuietly(response.getEntity());//在HttpClient 4.0+的版本,正确的关闭连接 } if (this.autoClose) this.close(); return html; } private String contentEncoding = "UTF-8"; public String getContentEncoding(){ return this.contentEncoding; } public void sleep(long seconds) { for (int i = 0; i < seconds; i++) { try { Thread.sleep(1000); System.out.print("."); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(""); } }
HttpUtil
import org.apache.http.Header; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.Map; import java.util.UUID; public class HttpUtil { public static Object getFirstValueFromRequest(String key, HttpServletRequest request){ Object value = null; //1. parameter value = request.getAttribute(key); if(value==null) value = request.getParameter(key); if(value==null) value = request.getHeader(key); if(value == null) value = getFirstCookie(key, request.getCookies()); return value; } /** * 从request中读取参数 * @param request * @param key * @return */ public static String getStringFromRequest(HttpServletRequest request, String key){ String value = (String)request.getAttribute(key); if(value==null) value = request.getParameter(key); if(value==null) value = request.getHeader(key); if(value==null) value = HttpUtil.getFirstCookie(key, request.getCookies()); return value; } /** * 从cookie中读取第一个符合的参数 * @param key * @param cookies * @return */ public static String getFirstCookie(String key,Cookie[] cookies){ if(cookies!=null && key!=null){ for(Cookie cookie : cookies){ if(key.equals(cookie.getName())) return cookie.getValue(); } } return null; } public static Map<String, Cookie> getCookieMap(HttpServletRequest request){ Map<String, Cookie> map = new HashMap<String, Cookie>(); Cookie[] cookies = request.getCookies(); if(cookies!=null){ for(Cookie cookie : cookies){ map.put( cookie.getName()+cookie.getPath(), (Cookie)cookie.clone()); } } return map; } public static String httpGet(String url, Map<String,Object> parameters)throws Exception{ HttpQuery httpQuery = new HttpQuery(); String html = httpQuery.httpGet(url, parameters); httpQuery.close(); return html; } public static Header[] httpGetHeaders(String url, Map<String,Object> parameters)throws Exception{ HttpQuery httpQuery = new HttpQuery(); httpQuery.setAutoClose(false); httpQuery.httpGet(url, parameters); Header[] headers = httpQuery.getHeaders(); return headers; } public static String httpPost(String url, Map<String,Object> parameters)throws Exception{ HttpQuery httpQuery = new HttpQuery(); String html = httpQuery.httpPost(url, parameters); return html; } public static String newAccessToken(){ String accessToken = UUID.randomUUID().toString(); return accessToken; } public static String getCookie(HttpServletRequest request, String cookieName){ Cookie[] cookies = request.getCookies(); for(Cookie cookie : cookies){ if(cookie.getName().trim().equals(cookieName.trim())) return cookie.getValue(); } return null; } public static String getRemortIP(HttpServletRequest request) { // 取代理ip地址 String ip = request.getHeader("x-forwarded-for"); // 取nginx代理设置的ip地址 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("x-real-ip"); } // 从网上取的 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } // 取JAVA获得的ip地址 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } // 去除unkonwn if (ip.startsWith("unknown")) { ip = ip.substring(ip.indexOf("unknown") + "unknown".length()); } // 去除多多余的信息 ip = ip.trim(); if (ip.startsWith(",")) { ip = ip.substring(1); } if (ip.indexOf(",") > 0) { ip = ip.substring(0, ip.indexOf(",")); } return ip; } }
Good luck!~
相关文章推荐
- httpClient post with cookie
- HttpClient使用SESSIONID模拟用户发送请求
- HttpClient使用SESSIONID模拟用户发送请求
- httpclient提示Cookie rejected: violates RFC 2109: domain must start with a dot
- HttpClient使用SESSIONID模拟用户发送请求
- IIS安全工具UrlScan介绍 ASP.NET 两种超强SQL 注入免费解决方案( 基于IIS,使用免费工具) 批改或隐藏IIS7.5的Server头信息 移除X-Powered-By,MVC,ASP.NET_SessionId 的 HTTP头或者cookie名称
- Struggling trying to get cookie out of response with HttpClient in .net 4.5
- HttpClient sessionID获取与设置
- [Angular] Protect The Session Id with https and http only
- HttpClient使用SESSIONID模拟用户发送请求
- 去除asp.net 2.0的会话cookie ASP.NET_SessionId 的httponly属性
- Tomcat7新特性?cookie HttpOnly的那些事(sessionid获取麻烦了)
- PHP HttpClient封装(可实现GET POST Cookie Session)
- [转]ASP.NET HttpModule for handling session end with StateServer
- ASP.NET中实现页面间的参数传递 QueryString/Application/Session/Cookie (转载)
- HTTP协议之Session和Cookie
- 使用HttpWebRequest提交ASP.NET表单并保持Session和Cookie
- ASP.NET中实现页面间的参数传递 QueryString\Application\Session\Cookie
- 使用HttpWebRequest提交ASP.NET表单并保持Session和Cookie
- 使用HttpWebRequest提交ASP.NET表单并保持Session和Cookie