1. // 旧版
  2. HttpClient httpClient = new DefaultHttpClient();
  3. // execute get/post/put or whatever
  4. httpClient.doGetPostPutOrWhatever();
  5. // get cookieStore
  6. CookieStore cookieStore = httpClient.getCookieStore();
  7. // get Cookies
  8. List<Cookie> cookies = cookieStore.getCookies();
  9. // process...
  1. // 新版
  2. /* init client */
  3. HttpClient http = null;
  4. CookieStore httpCookieStore = new BasicCookieStore();
  5. http = HttpClientBuilder.create().setDefaultCookieStore(httpCookieStore).build();
  6.  
  7. /* do stuff */
  8. HttpGet httpRequest = new HttpGet("http://stackoverflow.com/");
  9. HttpResponse httpResponse = null;
  10. try {httpResponse = http.execute(httpRequest);} catch (Throwable error) {throw new RuntimeException(error);}
  11.  
  12. /* check cookies */
  13. httpCookieStore.getCookies();
  1. // 我的
  2. @Test
  3. public void testGetCookies1() throws IOException {
  4. String result;
  5. // 获取文件 拼接
  6. String uri = bundle.getString("getCookies.uri");
  7. String getTestUrl = this.url + uri;
  8.  
  9. try {
  10.  
  11. BasicCookieStore cookieStore = new BasicCookieStore();
  12.  
  13. // 获取 响应
  14. HttpGet get = new HttpGet(getTestUrl);
  15. CloseableHttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
  16. // CloseableHttpClient build = HttpClientBuilder.create().build();
  17. // CloseableHttpResponse execute = build.execute(get);
  18. CloseableHttpResponse execute = httpClient.execute(get);
  19. result = EntityUtils.toString(execute.getEntity(), "utf-8");
  20. System.out.println(result);
  21.  
  22. // 获取cookies信息
  23. List<Cookie> cookies = cookieStore.getCookies();
  24. for (Cookie cookie : cookies) {
  25. String name = cookie.getName();
  26. String value = cookie.getValue();
  27. System.out.println("cookies: key= "+ name + " value= " + value);
  28. }
  29.  
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. } catch (ParseException e) {
  33. e.printStackTrace();
  34. }
  35.  
  36. }

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. 吴裕雄--天生自然 JAVASCRIPT开发学习:事件

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. SASS - 混合(Mixin)

    SASS – 简介 SASS – 环境搭建 SASS – 使用Sass程序 SASS – 语法 SASS – 变量 SASS- 局部文件(Partial) SASS – 混合(Mixin) SASS ...

  3. [GWCTF 2019]枯燥的抽奖

    0x00 知识点 种子爆破 工具 http://www.openwall.com/php_mt_seed 0x01 解题 查看源码进入check.php zOHF0Cxp49 <?php #这不 ...

  4. js保留的关键字

    js保留的关键字 break else new var case finally return void catch for switch while continue function this w ...

  5. Sequence Models Week 2 Operations on word vectors

    Operations on word vectors Welcome to your first assignment of this week! Because word embeddings ar ...

  6. 第21章—websocket

    spring boot 系列学习记录:http://www.cnblogs.com/jinxiaohang/p/8111057.html 码云源码地址:https://gitee.com/jinxia ...

  7. springBoot中的邮件发送

    1. 添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  8. bootstrap 支持的JavaScript插件

    一次性导入: Bootstrap提供了一个单一的文件,这个文件包含了Bootstrap的所有JavaScript插件,即bootstrap.js(压缩版本:bootstrap.min.js). 具体使 ...

  9. 吴裕雄--天生自然TensorFlow2教程:Tensor数据类型

    list: [1,1.2,'hello'] ,存储图片占用内存非常大 np.array,存成一个静态数组,但是numpy在深度学习之前就出现了,所以不适合深度学习 tf.Tensor,为了弥补nump ...

  10. PAT Advanced 1056 Mice and Rice (25) [queue的⽤法]

    题目 Mice and Rice is the name of a programming contest in which each programmer must write a piece of ...