// 旧版
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的更多相关文章

  1. Java通过httpclient获取cookie模拟登录

    package Step1; import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.Htt ...

  2. HttpClient获取Cookie的两种方式

    转载:http://blog.csdn.net/zhangbinu/article/details/72777620 一.旧版本的HttpClient获取Cookies p.s. 该方式官方已不推荐使 ...

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

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

  4. python打印cookies获取cookie

    def test_002_buy_ticket(self): data = [{"}] print(data) data = json.dumps(data) cookies = self. ...

  5. js获取cookie

    js获取cookie 之前用jQuery.cookie来获取cookie,虽然简单,但是项目上又多引用了一个插件,总觉得不太好,下面是我封装的js原生获取cookie的函数. function get ...

  6. js获取cookie中存储的值

    最近看了试卷题目发现自己会的十分的匮乏, 第一题就把自己难住了,知道有这个东西,但是实际上没有操作过. ========================================= cookie ...

  7. javascript设置和获取cookie的通用方法

    //获取cookie  function getCookieValue(cookieName)  {     var cookieValue = document.cookie;     var co ...

  8. 通过js获取cookie的实例及简单分析

    今天碰到一个在firefox下swfupload 上传时session不一致问题 在一个项目遇到多文件上传时,firefox下,服务器端的session获取不一致问题. 解决办法: 解决办法:将ses ...

  9. ASP.NET后台获取cookie中文乱码解决办法

    项目中有一功能,需要从一个页面前台使用cookie保存json数据,并传递到第二个页面.要在第二个页面中获取cookie中的json的值,没有任何处理情况下,获取的字符串为乱码,就连符号都是乱码的.百 ...

随机推荐

  1. part8 详情页面动态路由以及banner布局

    1.<router-link>标签 a标签就会把里面文字的颜色变掉 那我们可以换一种写法 <router-link tag='li'> //这样vue就会把这个标签渲染成li标 ...

  2. 合并排序_python

    #!/usr/bin/python # --coding:utf-8 -- def sort_merge(left,right): i,j=0,0 result=[] while i<len(l ...

  3. 转 SQL 的数据库 架构规范 之 58到家数据库30条军规解读

    军规适用场景:并发量大.数据量大的互联网业务 军规:介绍内容 解读:讲解原因,解读比军规更重要 一.基础规范 (1)必须使用InnoDB存储引擎 解读:支持事务.行级锁.并发性能更好.CPU及内存缓存 ...

  4. sort()函数使用详解

    使用时需要导入头文件<algorithm> #include<algorithm> 语法描述:sort(begin,end,cmp),cmp参数可以没有,如果没有默认非降序排序 ...

  5. Python Scrapy的QQ音乐爬虫 音乐下载、爬取歌曲信息、歌词、精彩评论

    QQ音乐爬虫(with scrapy)/QQ Music Spider UPDATE 2019.12.23 已实现对QQ音乐文件的下载,出于版权考虑,不对此部分代码进行公开.此项目仅作为学习交流使用, ...

  6. 学会拒绝,是一种智慧——OO电梯章节优化框架的思考

    在本章的三次作业里,每次作业我都有一个主题,分别是:托盘型共享数据.单步电梯运行优化.多部电梯运行优化,因而电梯优化实际是第二.三次作业.虽然后两次作业从性能分上看做得还不错,但阅读其他大佬博客,我深 ...

  7. runlevel 运行级别

    linux启动过程 关于Ubuntu 12.04修改默认运行级别,启动字符界面的个人理解   网上通常的做法是:(亲自试验,不管用),如果想直接操作请看绿色字体部分 (1)第一种方法:   由于Red ...

  8. idea启动服务连接mysql后 Navicat连接mysql就报错2013-Lost connection toMySQL server at

    我是使用navicat的windows端 连接centos下mysql服务器 第一次常规连接mysql正常,idea启动服务连接mysql后 Navicat连接mysql就报错2013-Lost co ...

  9. Python笔记_第四篇_高阶编程_高阶函数_3.sorted

    1. sorted函数: 常用的排序分:冒泡排序.选择排序.快速排序.插入排序.计数器排序 实例1:普通排序 # 普通排序 list1 = [,,,,] list2 = sorted(list1) # ...

  10. MQL4编程—值传递和引用传递

    定义 简单的说就是给一个变量或者对象取一个别名(引用和被引用的共享存储单元,要用修饰符&). 引用的用法 在MQL4中通过调试只接受引用作为函数参数的用法,在函数参数传递过程中,有两种传递方式 ...