博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Apache HttpComponents 获取Cookie
阅读量:7095 次
发布时间:2019-06-28

本文共 2430 字,大约阅读时间需要 8 分钟。

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
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(); } }}

 

转载地址:http://cgxql.baihongyu.com/

你可能感兴趣的文章
网站在架构时要考虑的事情
查看>>
MySQL修改root密码的多种方法
查看>>
android中WebView小结
查看>>
7、yum 学习笔记
查看>>
gentoo-livedvd安装
查看>>
myphpadmin导入SQL文件大小的限制
查看>>
linux内核源码目录结构
查看>>
linux 文本命令之----tr
查看>>
Failed to execute goal 拒绝访问
查看>>
HttpsURLConnection
查看>>
搞懂深度学习到底需要哪些数学知识
查看>>
java学习:理解final
查看>>
我的友情链接
查看>>
什么是区域链
查看>>
spring 手动 注入 销毁 bean
查看>>
Spring Boot 启动加载数据 CommandLineRunner
查看>>
AXIS2中OMElement和Java对象之间的转换 分享
查看>>
Manual Transaction 模式
查看>>
老男孩教育每日一题-第104天-如何查看linux系统时间?若当前系统时区不是中国,如何修改?...
查看>>
Hive 环境搭建
查看>>