zoukankan      html  css  js  c++  java
  • Apache HttpComponents 获取Cookie

    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();
            }
        }
    
    }
  • 相关阅读:
    使用C++与SFML编写一个简单的撞球游戏Part3——创建游戏启动界面
    生命游戏
    一道面试题
    为目标数字添加逗号分隔,由 baidu.number.comma 想到的
    制作JavaScript选择器(1)解析令牌
    Team Foundation 使用第三方比较工具
    Levenshtein Distance算法
    计算颜色的亮度值
    整理QUnit API
    Scrum笔记整理
  • 原文地址:https://www.cnblogs.com/daxin/p/3165139.html
Copyright © 2011-2022 走看看