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的值,没有任何处理情况下,获取的字符串为乱码,就连符号都是乱码的.百 ...
随机推荐
- idea 2019.3 最新破解补丁和激活码,可破解至2089年!
链接:https://blog.csdn.net/qq_42914528/article/details/85617901 上面方法失效了,请尝试以下方式激活(2020.1.6更新) idea激活码( ...
- MySQL 存储引擎(MyISAM、InnoDB、NDBCluster)
前言 MySQL 的存储引擎可能是所有关系型数据库产品中最具有特色的了,不仅可以同时使用多种存储引擎,而且每种存储引擎和MySQL之间使用插件方式这种非常松的耦合关系. 由于各存储引擎功能特性差异较大 ...
- kaggle——TMDB 电影票房收入预测
介绍 看电影是目前人们休闲娱乐,消遣时光的选择之一.我们都知道,有些电影的票房很高,有的电影票房却很低,那么决定票房的因素是什么呢?本次将介绍,如何根据电影上映前的一些信息来预测出该电影的票房. 知识 ...
- python 爬虫下载英语听力新闻(npr news)为mp3格式
想通过听实时新闻来提高英语听力,学了那么多年的英语,不能落下啊,不然白费背了那么多年的单词. npr news是美国国家公共电台,发音纯正,音频每日更新,以美国为主,世界新闻为辅,比如最近我国武汉发生 ...
- squid完全攻略
squid完全攻略 http://blog.sina.com.cn/s/blog_7572cf8e0100rl99.html squid,nginx,lighttpd反向代理的区别 [root@loc ...
- 25.docker compose 简介 和 docker-compose.yml 参数介绍
1. docker compose概念 文档 https://docs.docker.com/compose/compose-file/compose-versioning 一个基于 docker ...
- JS用例
showBtn :class="{getInput:showBtn}"v-if="showBtn" showBtn: true, this.showBtn = ...
- dbus探索
一.参考网址 1.Dbus组成和原理
- php速成_day1
一.概述 1.什么是PHP PHP ( Hypertext Preprocessor ),是英文超级文本预处理语言的缩写.PHP 是一种 跨平台.嵌入式的服务器端执行的描述语言,是一种在服务器端执行的 ...
- java网络考试系统的设计与实现 jsp 源码
开发环境: Windows操作系统开发工具:MyEclipse/Eclipse + JDK+ Tomcat + MySQL 数据库 项目简介: 网络考试系统主要用于实现高校在线考试,基本功能包括:自动 ...