最近项目中,用到了登陆后获取并携带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发起请求的更多相关文章

  1. 轻松把玩HttpClient之封装HttpClient工具类(五),携带Cookie的请求

    近期更新了一下HttpClientUtil工具类代码,主要是加入了一个參数HttpContext,这个是用来干嘛的呢?事实上是用来保存和传递Cookie所须要的. 由于我们有非常多时候都须要登录.然后 ...

  2. python接口自动化测试——简单的文件上传代码实现,人人网登陆后上传图片举例

    import requests '''人人网登陆,图片上传 '''def loginData(): '''登录请求参数''' data = { 'email': '13484545195', 'ico ...

  3. python接口自动化测试七:获取登录的Cookies,并关联到下一个请求

    获取登录的cookies:loginCookies = r.cookies 把获取到的cookies传入请求:cookies=loginCookies 此方法需每一次都调用登录方法,并且每一次发送请求 ...

  4. python接口自动化测试七:获取登录的Cookies

    python接口自动化测试七:获取登录的Cookies,并关联到下一个请求   获取登录的cookies:loginCookies = r.cookies 把获取到的cookies传入请求:cooki ...

  5. Node.js:实现知乎(www.zhihu.com)模拟登陆,获取用户关注主题

    前一段时间,在瞎看看 Node.js,便研究通过 Node.js 实现知乎模拟登陆.相信,有很多网站有登陆权限设置,如若用户未登陆,将会跳转至首页提醒用户登陆,无法浏览部分页面. 如若是 b/s 架构 ...

  6. (26)基于cookie的登陆认证(写入cookie、删除cookie、登陆后所有域下的网页都可访问、登陆成功跳转至用户开始访问的页面、使用装饰器完成所有页面的登陆认证)

    获取cookie request.COOKIES['key'] request.get_signed_cookie(key, default=RAISE_ERROR, salt='', max_age ...

  7. 第三百三十三节,web爬虫讲解2—Scrapy框架爬虫—Scrapy模拟浏览器登录—获取Scrapy框架Cookies

    第三百三十三节,web爬虫讲解2—Scrapy框架爬虫—Scrapy模拟浏览器登录 模拟浏览器登录 start_requests()方法,可以返回一个请求给爬虫的起始网站,这个返回的请求相当于star ...

  8. 十二 web爬虫讲解2—Scrapy框架爬虫—Scrapy模拟浏览器登录—获取Scrapy框架Cookies

    模拟浏览器登录 start_requests()方法,可以返回一个请求给爬虫的起始网站,这个返回的请求相当于start_urls,start_requests()返回的请求会替代start_urls里 ...

  9. 记一次HTTPClient模拟登录获取Cookie的开发历程

    记一次HTTPClient模拟登录获取Cookie的开发历程 环境: ​ springboot : 2.7 ​ jdk: 1.8 ​ httpClient : 4.5.13 设计方案 ​ 通过新建一个 ...

随机推荐

  1. Local Notification

    大家都知道Push Notification,这个东西需要联网才可以用.iOS4起,苹果引入了一种可以在设备内部引发的notification.不需要复杂的服务器编程,或其他复杂的配置.这个技术就是L ...

  2. ObjC正则表达式验证

    试过ObjC的regkit这个框架. 也用过内置的正则表达式验证. 最后发现有个非常简单的方法就可以做到验证正则表达式.那就是NSPredicte这个类提供的方法. 这里有验证邮箱地址的正则为例: N ...

  3. Mybatis 多个Mapper

    在实际应用中的,会有较多个mapper.如果每新建一个mapper,就向SqlMapConfig上加上对应的配置文件,会十分不便. 可以新建一个package,在其下面放置Mapper.java,同时 ...

  4. makeword()

    MAKEWORD(学习之用,转载) MAKEWORD 宏 平台:SDK 这个宏创建一个被指定变量连接而成的WORD变量.返回一个WORD变量. (注:typedef unsigned short WO ...

  5. Mysql主主复制+keepalived

    1>环境 Master1 10.0.0.201 Master2 10.0.0.202 2>Master1操作授权 mysql>grant replication slave on * ...

  6. log4net 入门使用

    log4net 是dotnet平台下的一个日记记录组件. 一  NuGet中安装log4net包: 二 配置log4net.config文件 配置文件内容: <?xml version=&quo ...

  7. 获取服务端https证书

    最近开发一个需求,涉及获取服务端https证书.一般进行https调用我们都不太关心底层细节,直接使用WebClient或者HttpWebRequest来发送请求,这两种方法都无法获取证书信息,需要用 ...

  8. windows Win7如何设置多用户同时远程登录

    windows  Win7如何设置多用户同时远程登录 1.创建一个用户 密码永不过期 2.在本地组策略编辑器里面,依次展开计算机配置--->管理模板--->Windows组件---> ...

  9. 「HNOI 2013」游走

    题目链接 戳我 \(Solution\) 首先申明几个变量: f[x]:到点x的概率, vis[x]:x点的度 dp[x][y]:(x,y)这条边的概率 number[x][y]:x这条边的编号 下面 ...

  10. 526. Beautiful Arrangement

    Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is const ...