Android的BasicCookieStore,饼干和HTTPGET [英] Android BasicCookieStore, Cookies and HttpGet

查看:681
本文介绍了Android的BasicCookieStore,饼干和HTTPGET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应该发送GET请求一个URL,并与它一起发送一些饼干的应用程序。我一直在寻找的BasicCookieStore和饼干类的几个code的例子,但我无法弄清楚如何使用它们。任何人都可以点我在正确的方向?

I have a an app that should send a GET request to a URL and send some cookies along with it. I've been looking at a few code examples for BasicCookieStore and Cookie classes, but I'm not able to figure out how to use them. Can anyone point me in the right direction?

推荐答案

要使用cookie你需要的线沿线的东西:

To use cookies you need something along the lines of:

CookieStore cookieStore = new BasicCookieStore();
DefaultHttpClient httpclient = new DefaultHttpClient();

HttpContext ctx = new BasicHttpContext();
ctx.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpGet get = new HttpGet("your URL here");

HttpResponse response = httpclient.execute(get,ctx);

如果你想保持请求之间的cookie,你必须重复使用的CookieStore CTX 为每个请求。

此外,您还可以读取你的的CookieStore 来看看里面是什么:

Also, you may read your cookieStore to see what's inside:

List<Cookie> cookies = cookieStore.getCookies();
if( !cookies.isEmpty() ){
    for (Cookie cookie : cookies){
        String cookieString = cookie.getName() + " : " + cookie.getValue();
        Log.info(TAG, cookieString);
    }
}

这篇关于Android的BasicCookieStore,饼干和HTTPGET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆