HttpClient登陆后获取并携带cookies发起请求
最近项目中,用到了登陆后获取并携带cookies发起请求的业务场景,现总结写出来备忘一下。
1.定义存取cookies信息的全局变量
public class HttpUtil { /**
* 用来存取cookies信息的变量.
*/
private static CookieStore cookieStore;
2.登陆后获取cookies信息存储到全局变量
public class HttpUtil { /**
*
* @param httpUrl
* 地址
* @param paramMap
* 参数
*
*/
public static String loginPost(String httpUrl, Map<String, Object> paramMap) throws Exception {
if (log.isInfoEnabled()) {
log.info("请求httpUrl=" + httpUrl);
log.info("请求params=" + JSONObject.toJSONString(paramMap));
} // 创建httpPost
HttpPost httpPost = new HttpPost(httpUrl); // 设置参数
List<NameValuePair> params = new ArrayList<>();
if (paramMap != null && paramMap.size() > 0) {
Set<String> keySet = paramMap.keySet();
for (String key : keySet) {
params.add(new BasicNameValuePair(key, UcopCommonUtil.convertString(paramMap.get(key))));
}
httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
} String response;
DefaultHttpClient httpClient = null;
try {
httpClient = new DefaultHttpClient();
String contentCharset = CoreProtocolPNames.HTTP_CONTENT_CHARSET;
String soTimeOut = CoreConnectionPNames.SO_TIMEOUT;
String connectTimeOut = CoreConnectionPNames.CONNECTION_TIMEOUT;
httpClient.getParams().setParameter(contentCharset, "utf-8");
httpClient.getParams().setParameter(soTimeOut, 60000);
httpClient.getParams().setParameter(connectTimeOut, 60000); HttpResponse httpResponse = httpClient.execute(httpPost); // 获得返回结果
response = EntityUtils.toString(httpResponse.getEntity(), UcopUtils.UCOP_DEFAULT_CHARSET); // 获得cookie并存取
this.cookieStore = httpClient.getCookieStore();
List<Cookie> cookieList = this.cookieStore.getCookies();
for (Cookie cookie : cookieList){
String name =cookie.getName();
String value = cookie.getValue();
log.info("登陆成功,cookie name = " + name + ", cookie value = " + value);
}
if (log.isInfoEnabled()) {
log.info("响应内容=" + response);
}
return response;
} catch (Exception e) {
log.error("HttpPost请求失败:" + e.getMessage(), e);
throw new RuntimeException("HttpPost请求失败:" + e.getMessage(), e);
} finally {
if (null != httpClient) {
httpClient.getConnectionManager().shutdown();
}
}
}
3.携带cookie发送业务请求
public class HttpUtil { /**
*
* @param url 请求地址
* @param paramMap 请求内容
* @return 响应内容
* @throws Exception
*/
public static String requestPostWithCookie(String url, Map<String, Object> paramMap) throws Exception {
if (log.isInfoEnabled()) {
log.info("post请求地址:" + url);
log.info("post请求内容:" + paramMap);
} long beginTime = System.currentTimeMillis();
try {
HttpClient httpClient = HttpClients.custom().setDefaultCookieStore(this.cookieStore).build();
HttpPost httpPost = new HttpPost(url); // 设置参数
List<NameValuePair> params = new ArrayList<>();
if (paramMap != null && paramMap.size() > 0) {
Set<String> keySet = paramMap.keySet();
for (String key : keySet) {
params.add(new BasicNameValuePair(key, UcopCommonUtil.convertString(paramMap.get(key))));
}
httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
} // 设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(120000).setConnectTimeout(120000).build();
httpPost.setConfig(requestConfig); // 发起请求
HttpResponse response = httpClient.execute(httpPost); // 获取响应数据并转换成JSON
String resString = EntityUtils.toString(response.getEntity(), UcopUtils.UCOP_DEFAULT_CHARSET);
log.info("post响应内容: " + resString); return resString;
} catch (Exception e) {
log.error("请求失败:" + e.getMessage(), e);
throw e;
} finally {
log.info("结束post请求,耗时(ms):" + (System.currentTimeMillis() - beginTime));
}
}
4.完整示例代码
package com.zat.ucop.bus.service.impl; import com.alibaba.fastjson.JSONObject;
import com.zat.ucop.service.util.UcopCommonUtil;
import com.zat.ucop.service.util.UcopUtils;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set; public class HttpUtil { private static Logger log = LoggerFactory.getLogger(HttpUtil.class); /**
* 用来存取cookies信息的变量.
*/
private static CookieStore cookieStore; /**
*
* @param httpUrl
* 地址
* @param paramMap
* 参数
*
*/
public static String loginPost(String httpUrl, Map<String, Object> paramMap) throws Exception {
if (log.isInfoEnabled()) {
log.info("请求httpUrl=" + httpUrl);
log.info("请求params=" + JSONObject.toJSONString(paramMap));
} // 创建httpPost
HttpPost httpPost = new HttpPost(httpUrl); // 设置参数
List<NameValuePair> params = new ArrayList<>();
if (paramMap != null && paramMap.size() > 0) {
Set<String> keySet = paramMap.keySet();
for (String key : keySet) {
params.add(new BasicNameValuePair(key, UcopCommonUtil.convertString(paramMap.get(key))));
}
httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
} String response;
DefaultHttpClient httpClient = null;
try {
httpClient = new DefaultHttpClient();
String contentCharset = CoreProtocolPNames.HTTP_CONTENT_CHARSET;
String soTimeOut = CoreConnectionPNames.SO_TIMEOUT;
String connectTimeOut = CoreConnectionPNames.CONNECTION_TIMEOUT;
httpClient.getParams().setParameter(contentCharset, "utf-8");
httpClient.getParams().setParameter(soTimeOut, 60000);
httpClient.getParams().setParameter(connectTimeOut, 60000); HttpResponse httpResponse = httpClient.execute(httpPost); // 获得返回结果
response = EntityUtils.toString(httpResponse.getEntity(), UcopUtils.UCOP_DEFAULT_CHARSET); // 获得cookie并存取
cookieStore = httpClient.getCookieStore();
List<Cookie> cookieList = cookieStore.getCookies();
for (Cookie cookie : cookieList){
String name =cookie.getName();
String value = cookie.getValue();
log.info("登陆成功,cookie name = " + name + ", cookie value = " + value);
}
if (log.isInfoEnabled()) {
log.info("响应内容=" + response);
}
return response;
} catch (Exception e) {
log.error("HttpPost请求失败:" + e.getMessage(), e);
throw new RuntimeException("HttpPost请求失败:" + e.getMessage(), e);
} finally {
if (null != httpClient) {
httpClient.getConnectionManager().shutdown();
}
}
} /**
*
* @param url 请求地址
* @param paramMap 请求内容
* @return 响应内容
* @throws Exception
*/
public static String requestPostWithCookie(String url, Map<String, Object> paramMap) throws Exception {
if (log.isInfoEnabled()) {
log.info("post请求地址:" + url);
log.info("post请求内容:" + paramMap);
} long beginTime = System.currentTimeMillis();
try {
HttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
HttpPost httpPost = new HttpPost(url); // 设置参数
List<NameValuePair> params = new ArrayList<>();
if (paramMap != null && paramMap.size() > 0) {
Set<String> keySet = paramMap.keySet();
for (String key : keySet) {
params.add(new BasicNameValuePair(key, UcopCommonUtil.convertString(paramMap.get(key))));
}
httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
} // 设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(120000).setConnectTimeout(120000).build();
httpPost.setConfig(requestConfig); // 发起请求
HttpResponse response = httpClient.execute(httpPost); // 获取响应数据并转换成JSON
String resString = EntityUtils.toString(response.getEntity(), UcopUtils.UCOP_DEFAULT_CHARSET);
log.info("post响应内容: " + resString); return resString;
} catch (Exception e) {
log.error("请求失败:" + e.getMessage(), e);
throw e;
} finally {
log.info("结束post请求,耗时(ms):" + (System.currentTimeMillis() - beginTime));
}
} }
补充:HttpClient获取Cookie的两种方式
1.使用DefaultHttpClient类实例化httpClient对象:
package com.zat.ucop.bus.service.impl; import com.alibaba.fastjson.JSONObject;
import com.zat.ucop.service.util.UcopCommonUtil;
import com.zat.ucop.service.util.UcopUtils;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set; public class HttpUtil { private static Logger log = LoggerFactory.getLogger(HttpUtil.class); /**
* 用来存取cookies信息的变量.
*/
private static CookieStore cookieStore; /**
*
* @param httpUrl
* 地址
* @param paramMap
* 参数
*
*/
public static String loginPost(String httpUrl, Map<String, Object> paramMap) throws Exception {
if (log.isInfoEnabled()) {
log.info("请求httpUrl=" + httpUrl);
log.info("请求params=" + JSONObject.toJSONString(paramMap));
} // 创建httpPost
HttpPost httpPost = new HttpPost(httpUrl); // 设置参数
List<NameValuePair> params = new ArrayList<>();
if (paramMap != null && paramMap.size() > 0) {
Set<String> keySet = paramMap.keySet();
for (String key : keySet) {
params.add(new BasicNameValuePair(key, UcopCommonUtil.convertString(paramMap.get(key))));
}
httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
} String response;
DefaultHttpClient httpClient = null;
try {
httpClient = new DefaultHttpClient();
String contentCharset = CoreProtocolPNames.HTTP_CONTENT_CHARSET;
String soTimeOut = CoreConnectionPNames.SO_TIMEOUT;
String connectTimeOut = CoreConnectionPNames.CONNECTION_TIMEOUT;
httpClient.getParams().setParameter(contentCharset, "utf-8");
httpClient.getParams().setParameter(soTimeOut, 60000);
httpClient.getParams().setParameter(connectTimeOut, 60000); HttpResponse httpResponse = httpClient.execute(httpPost); // 获得返回结果
response = EntityUtils.toString(httpResponse.getEntity(), UcopUtils.UCOP_DEFAULT_CHARSET); // 获得cookie并存取
82 cookieStore = httpClient.getCookieStore();
83 List<Cookie> cookieList = cookieStore.getCookies();
84 for (Cookie cookie : cookieList){
85 String name =cookie.getName();
86 String value = cookie.getValue();
87 log.info("登陆成功,cookie name = " + name + ", cookie value = " + value);
88 }
if (log.isInfoEnabled()) {
log.info("响应内容=" + response);
}
return response;
} catch (Exception e) {
log.error("HttpPost请求失败:" + e.getMessage(), e);
throw new RuntimeException("HttpPost请求失败:" + e.getMessage(), e);
} finally {
if (null != httpClient) {
httpClient.getConnectionManager().shutdown();
}
}
} }
2.使用CloseableHttpClient类实例化httpClient对象:
package com.zat.ucop.bus.service.impl; import com.alibaba.fastjson.JSONObject;
import com.zat.ucop.service.util.UcopCommonUtil;
import com.zat.ucop.service.util.UcopUtils;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set; public class HttpUtil { private static Logger log = LoggerFactory.getLogger(HttpUtil.class); /**
* 用来存取cookies信息的变量.
*/
private static CookieStore cookieStore; /**
*
* @param url 请求地址
* @param paramMap 请求内容
* @return 响应内容
* @throws Exception
*/
public static String requestPostWithCookie(String url, Map<String, Object> paramMap) throws Exception {
if (log.isInfoEnabled()) {
log.info("post请求地址:" + url);
log.info("post请求内容:" + paramMap);
} long beginTime = System.currentTimeMillis();
try {
CookieStore cookieStore = new BasicCookieStore();
HttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
HttpPost httpPost = new HttpPost(url); // 设置参数
List<NameValuePair> params = new ArrayList<>();
if (paramMap != null && paramMap.size() > 0) {
Set<String> keySet = paramMap.keySet();
for (String key : keySet) {
params.add(new BasicNameValuePair(key, UcopCommonUtil.convertString(paramMap.get(key))));
}
httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
} // 设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(120000).setConnectTimeout(120000).build();
httpPost.setConfig(requestConfig); // 发起请求
HttpResponse response = httpClient.execute(httpPost); 74 List<Cookie> cookieList = cookieStore.getCookies();
75 for (Cookie cookie : cookieList){
76 String name =cookie.getName();
77 String value = cookie.getValue();
78 log.info("cookie name = " + name + ", cookie value = " + value);
79 } // 获取响应数据并转换成JSON
String resString = EntityUtils.toString(response.getEntity(), UcopUtils.UCOP_DEFAULT_CHARSET);
log.info("post响应内容: " + resString); return resString;
} catch (Exception e) {
log.error("请求失败:" + e.getMessage(), e);
throw e;
} finally {
log.info("结束post请求,耗时(ms):" + (System.currentTimeMillis() - beginTime));
}
} }
HttpClient登陆后获取并携带cookies发起请求的更多相关文章
- 轻松把玩HttpClient之封装HttpClient工具类(五),携带Cookie的请求
近期更新了一下HttpClientUtil工具类代码,主要是加入了一个參数HttpContext,这个是用来干嘛的呢?事实上是用来保存和传递Cookie所须要的. 由于我们有非常多时候都须要登录.然后 ...
- python接口自动化测试——简单的文件上传代码实现,人人网登陆后上传图片举例
import requests '''人人网登陆,图片上传 '''def loginData(): '''登录请求参数''' data = { 'email': '13484545195', 'ico ...
- python接口自动化测试七:获取登录的Cookies,并关联到下一个请求
获取登录的cookies:loginCookies = r.cookies 把获取到的cookies传入请求:cookies=loginCookies 此方法需每一次都调用登录方法,并且每一次发送请求 ...
- python接口自动化测试七:获取登录的Cookies
python接口自动化测试七:获取登录的Cookies,并关联到下一个请求 获取登录的cookies:loginCookies = r.cookies 把获取到的cookies传入请求:cooki ...
- Node.js:实现知乎(www.zhihu.com)模拟登陆,获取用户关注主题
前一段时间,在瞎看看 Node.js,便研究通过 Node.js 实现知乎模拟登陆.相信,有很多网站有登陆权限设置,如若用户未登陆,将会跳转至首页提醒用户登陆,无法浏览部分页面. 如若是 b/s 架构 ...
- (26)基于cookie的登陆认证(写入cookie、删除cookie、登陆后所有域下的网页都可访问、登陆成功跳转至用户开始访问的页面、使用装饰器完成所有页面的登陆认证)
获取cookie request.COOKIES['key'] request.get_signed_cookie(key, default=RAISE_ERROR, salt='', max_age ...
- 第三百三十三节,web爬虫讲解2—Scrapy框架爬虫—Scrapy模拟浏览器登录—获取Scrapy框架Cookies
第三百三十三节,web爬虫讲解2—Scrapy框架爬虫—Scrapy模拟浏览器登录 模拟浏览器登录 start_requests()方法,可以返回一个请求给爬虫的起始网站,这个返回的请求相当于star ...
- 十二 web爬虫讲解2—Scrapy框架爬虫—Scrapy模拟浏览器登录—获取Scrapy框架Cookies
模拟浏览器登录 start_requests()方法,可以返回一个请求给爬虫的起始网站,这个返回的请求相当于start_urls,start_requests()返回的请求会替代start_urls里 ...
- 记一次HTTPClient模拟登录获取Cookie的开发历程
记一次HTTPClient模拟登录获取Cookie的开发历程 环境: springboot : 2.7 jdk: 1.8 httpClient : 4.5.13 设计方案 通过新建一个 ...
随机推荐
- 编写高质量代码改善C#程序的157个建议——建议73:避免锁定不恰当的同步对象
建议73:避免锁定不恰当的同步对象 在C#中,让线程同步的另一种编码方式就是使用线程锁.线程锁的原理,就是锁住一个资源,使得应用程序在此刻只有一个线程访问该资源.通俗地讲,就是让多线程变成单线程.在C ...
- Canvas vs. SVG[转]
Canvas 和 SVG 都允许您在浏览器中创建图形,但是它们在根本上是不同的. SVG SVG 是一种使用 XML 描述 2D 图形的语言. SVG 基于 XML,这意味着 SVG DOM 中的每个 ...
- How to count the number of threads in a process on Linux
If you want to see the number of threads per process in Linux environments, there are several ways t ...
- replaceState 实现返回从新定位
在web 开发中,选择列表分类,在中商品, 详情页面后,返回的时候我们想定位到原来选择的分类 就需要借助window.history.replaceState来实现 function getProdu ...
- 转:javascript判断IE浏览器
http://blog.csdn.net/ranbolwb/article/details/18555847 function isIE() { //ie? if (!!window.ActiveXO ...
- Cordova deploy on Android
网上找了几篇Phonegap在安卓上的部署,版本都比较老了,不过还是部署成功了, 写篇博客以做纪录. 1.先下载IDE:戳 2.下载Phonegap:戳 3.启动ADT,新建普通Android App ...
- Apache启动报错Address already in use: make_sock: could not bind to...
Apache启动时报错:(98)Address already in use: make_sock: could not bind to... # /etc/init.d/httpd start St ...
- day04.4-装饰器
1. 装饰器 装饰器本身就是函数,功能是为其他函数添加附加功能 装饰器的设计原则: (a). 不修改被修饰函数的源代码: (b). 不修改被修改函数的调用方法. 装饰器=高阶函数+函数嵌套+闭包 装饰 ...
- Chat Order (map映射)
Chat Order Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit ...
- HTML中input和button设置同样高度却不能等高的原因
同样设置35px,input略显高: input加个样式就行 box-sizing: border-box;