HttpClient怎么获取cookie
// 旧版
HttpClient httpClient = new DefaultHttpClient();
// execute get/post/put or whatever
httpClient.doGetPostPutOrWhatever();
// get cookieStore
CookieStore cookieStore = httpClient.getCookieStore();
// get Cookies
List<Cookie> cookies = cookieStore.getCookies();
// process...
// 新版
/* init client */
HttpClient http = null;
CookieStore httpCookieStore = new BasicCookieStore();
http = HttpClientBuilder.create().setDefaultCookieStore(httpCookieStore).build(); /* do stuff */
HttpGet httpRequest = new HttpGet("http://stackoverflow.com/");
HttpResponse httpResponse = null;
try {httpResponse = http.execute(httpRequest);} catch (Throwable error) {throw new RuntimeException(error);} /* check cookies */
httpCookieStore.getCookies();
// 我的
@Test
public void testGetCookies1() throws IOException {
String result;
// 获取文件 拼接
String uri = bundle.getString("getCookies.uri");
String getTestUrl = this.url + uri; try { BasicCookieStore cookieStore = new BasicCookieStore(); // 获取 响应
HttpGet get = new HttpGet(getTestUrl);
CloseableHttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
// CloseableHttpClient build = HttpClientBuilder.create().build();
// CloseableHttpResponse execute = build.execute(get);
CloseableHttpResponse execute = httpClient.execute(get);
result = EntityUtils.toString(execute.getEntity(), "utf-8");
System.out.println(result); // 获取cookies信息
List<Cookie> cookies = cookieStore.getCookies();
for (Cookie cookie : cookies) {
String name = cookie.getName();
String value = cookie.getValue();
System.out.println("cookies: key= "+ name + " value= " + value);
} } catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} }
https://stackoverflow.com/questions/8733758/how-can-i-get-the-cookies-from-httpclient
HttpClient怎么获取cookie的更多相关文章
- Java通过httpclient获取cookie模拟登录
package Step1; import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.Htt ...
- HttpClient获取Cookie的两种方式
转载:http://blog.csdn.net/zhangbinu/article/details/72777620 一.旧版本的HttpClient获取Cookies p.s. 该方式官方已不推荐使 ...
- 记一次HTTPClient模拟登录获取Cookie的开发历程
记一次HTTPClient模拟登录获取Cookie的开发历程 环境: springboot : 2.7 jdk: 1.8 httpClient : 4.5.13 设计方案 通过新建一个 ...
- python打印cookies获取cookie
def test_002_buy_ticket(self): data = [{"}] print(data) data = json.dumps(data) cookies = self. ...
- js获取cookie
js获取cookie 之前用jQuery.cookie来获取cookie,虽然简单,但是项目上又多引用了一个插件,总觉得不太好,下面是我封装的js原生获取cookie的函数. function get ...
- js获取cookie中存储的值
最近看了试卷题目发现自己会的十分的匮乏, 第一题就把自己难住了,知道有这个东西,但是实际上没有操作过. ========================================= cookie ...
- javascript设置和获取cookie的通用方法
//获取cookie function getCookieValue(cookieName) { var cookieValue = document.cookie; var co ...
- 通过js获取cookie的实例及简单分析
今天碰到一个在firefox下swfupload 上传时session不一致问题 在一个项目遇到多文件上传时,firefox下,服务器端的session获取不一致问题. 解决办法: 解决办法:将ses ...
- ASP.NET后台获取cookie中文乱码解决办法
项目中有一功能,需要从一个页面前台使用cookie保存json数据,并传递到第二个页面.要在第二个页面中获取cookie中的json的值,没有任何处理情况下,获取的字符串为乱码,就连符号都是乱码的.百 ...
随机推荐
- spring boot rest 接口集成 spring security(1) - 最简配置
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- 福州大学2020年春软工实践W班第一次作业
作业描述 这个作业属于哪个课程 福州大学2020年春软工实践W班 这个作业要求在哪里 寒假作业(1/2) 这个作业的目标 建立博客.回顾,我的初心.当下和未来.学习路线 作业正文 福州大学2020年春 ...
- 正则表达式模式修正符 比如/esi
正则表达式模式修正符 比如/esi 作者: 字体:[增加 减小] 类型:转载 下面列出了当前在 PCRE 中可能使用的修正符.括号中是这些修正符的内部 PCRE 名.修正符中的空格和换行被忽略,其它字 ...
- 2019年阿里java面试题
一.JVM与性能优化 描述一下 JVM 加载 Class 文件的原理机制? 什么是类加载器? 类加载器有哪些? 什么是tomcat类加载机制? 类加载器双亲委派模型机制? Java 内存分配? Jav ...
- ZJNU 2345 - 小Y的方格纸
明显,总共有n*m格,已经涂了k格 所以剩下n*m-k格 如果n*m-k<=k,即k已经占用了大于等于一半的格子,显然答案为0 否则 剩下的格子中取k+1,k+2...n*m-k格均可 取组合数 ...
- 26. docker compose 的安装 和 基本使用
1. 安装 docker compose https://docs.docker.com/compose/install/ 选择linux 即可 sudo curl -L "https:/ ...
- Redis_大保健
Redis redis命令参考网址: http://doc.redisfans.com/ redis主从: 集群:一组通过网络连接的计算机,共同对外提供服务,像一个独立的服务器. 一.简介 nosql ...
- jquery如何获取div下ul的某个li
$('div ul').each(function(){ alert($(this).find('li').eq(x)) }) $("div ul li:eq(1)")// $(& ...
- subprocess.Popen stdout重定向内容实时获取
python 打开一个新进程执行系统命令, test 执行完才能获取返回, test1 实时获取返回结果 import subprocess def test(cmd): p = subprocess ...
- Ubuntu---不能打开 exfat 文件系统格式的 U盘解决方法
出现问题:今天把 U 盘插入 Ubuntu 系统的电脑中,打开 U 盘发现弹出 系统格式不支持 的提醒,无法进入 U 盘进行操作. 环境: Ubuntu18.04 TSL; 格式化为 exfat 文件 ...