package org.apache.http.examples.client;

import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils; /**
* This example demonstrates the use of a local HTTP context populated with
* custom attributes.
*/
public class ClientCustomContext { public final static void main(String[] args) throws Exception { HttpClient httpclient = new DefaultHttpClient();
try {
// Create a local instance of cookie store
CookieStore cookieStore = new BasicCookieStore(); // Create local HTTP context
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpGet httpget = new HttpGet("http://www.google.com/"); System.out.println("executing request " + httpget.getURI()); // Pass local context as a parameter
HttpResponse response = httpclient.execute(httpget, localContext);
HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
List<Cookie> cookies = cookieStore.getCookies();
for (int i = 0; i < cookies.size(); i++) {
System.out.println("Local cookie: " + cookies.get(i));
} // Consume response content
EntityUtils.consume(entity); System.out.println("----------------------------------------"); } finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
} }

Apache HttpComponents 获取Cookie的更多相关文章

  1. Apache HttpComponents 获取inputStream

    package org.apache.http.examples.client; import java.io.IOException; import java.io.InputStream; imp ...

  2. Apache HttpComponents 获取页面内容String方式

    /* * ==================================================================== * Licensed to the Apache S ...

  3. Apache HttpComponents中的cookie匹配策略

    Apache HttpComponents中的cookie匹配策略 */--> pre.src {background-color: #292b2e; color: #b2b2b2;} pre. ...

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

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

  5. Apache HttpComponents 工具类 [ HttpUtil ]

    pom.xml <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId&g ...

  6. Cookie的使用、Cookie详解、HTTP cookies 详解、获取cookie的方法、客户端获取Cookie、深入解析cookie

    Cookie是指某些网站为了辨别用户身份.进行session跟踪而存储在用户本地终端上的数据(通常经过加密),比如说有些网站需要登录才能访问某个页面,在登录之前,你想抓取某个页面内容是不允许的.那么我 ...

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

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

  8. js获取cookie

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

  9. js获取cookie中存储的值

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

随机推荐

  1. url-pattern / /*匹配

    http://hi.baidu.com/atell/item/522112d3db45081fd90e44e1 struts2中配置为 <url-pattern>/*</url-pa ...

  2. Falsk-信号

    Flask框架中的信号基于blinker,其主要就是让开发者可是在flask请求过程中定制一些用户行为. 安装:pip3 install blinker request_started = _sign ...

  3. DBA_实践指南系列5_Oracle Erp R12日常运维和管理(案例)

    2013-12-05 Created By BaoXinjian

  4. 安装配置PhoneGap开发环境(一)

    1 安装JDK 略. 2 安装Eclipse并安装ADT插件 Android的官网提供集成了Android插件的Eclipse开发环境. 眼下訪问不了,尝试百度一下其他资源. 3 安装Android  ...

  5. 基于Vuejs实现 Skeleton Loading 骨架图

    原文地址:https://cloud.tencent.com/developer/article/1006169 https://mp.weixin.qq.com/s/qmyn6mGrO6hRKuvK ...

  6. 转 CAS实现SSO单点登录原理

    原文链接   http://m.blog.csdn.net/hxpjava1/article/details/74019017 CAS 简介 1. 1.1.  What is CAS ? CAS (  ...

  7. Java:多线程,线程池,用Executors静态工厂生成常用线程池

    一: newSingleThreadExecutor 创建一个单线程的线程池,以无界队列方式运行.这个线程池只有一个线程在工作(如果这个唯一的线程因为异常结束,那么会有一个新的线程来替代它.)此线程池 ...

  8. java.lang.IllegalStateException: The specified child already has a parent. You must call removeView

     java.lang.IllegalStateException: The specified child already has a parent. You must call removeVi ...

  9. ubuntu下gcc-avr安装

    ubuntu下研究arduino时发现,原来可以不用arduino IDE开发,linux下还有gcc-avr直接开发avr系列的控制器. 于是,迫不及待的查看了下相关资料,总结一下安装gcc-avr ...

  10. thread-local-allocation-buffers

    https://www.azul.com/files/Whats-inside-a-JVM-webinar-presentation.pdf https://www.zhihu.com/questio ...