Android的HttpClient的永久性Cookie [英] Android HttpClient persistent cookies

查看:193
本文介绍了Android的HttpClient的永久性Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:这个问题和它的答案,不应该再被推荐给任何人阅读。 的Andr​​oid没有再建议的HttpClient(读:德precated) ,而是建议HttpURLConnection的。图书馆的一个很好的例子,现在使用的,是改造和的 OkHttp 。在这个问题的背景下,曲奇可以保存,储存,交付与随后的查询。这不是透明地处理。随着 OkHttp可以使用拦截

UPDATE: This question and its answers should no longer be recommended to anyone reading this. Android no-longer recommends HttpClient (read: deprecated), and instead recommends HttpUrlConnection. A good example of libraries to use now, are Retrofit and OkHttp. In the context of this question, cookies can be saved, stored and delivered with subsequent queries. This is not handled transparently. With OkHttp you can use Interceptors.

我有一个Android应用程序与多个意向。

I have an Android application with multiple intents.

第一个目的是登录表单,随后的意图依靠从登录过程中提供饼干。

The first intent is a login form, subsequent intents rely on cookies provided from the login process.

这是我遇到的问题是,饼干不会出现跨越意图要坚持。我在每一个意图创造新的HttpClients(我最初试图Parcelable传达给每一个意图,而没有工作这么好发射的话)。

The problem that I am having is that cookies do not appear to be persisting across the intents. I am creating new HttpClients in each intent (I initially tried to Parcelable transmit it across to each intent, which did not work so well).

有没有人有做饼干任何提示持续跨越意图?

Does anyone have any tips for making cookies persist across intents?

推荐答案

您可以做@Emmanuel建议,也可以通过<一个href="http://developer.android.com/reference/org/apache/http/protocol/BasicHttpContext.html">BasicHttpContext在HttpClients之间正在创建。

You can do what @Emmanuel suggested or you can pass the BasicHttpContext between the HttpClients you are creating.

示例使用情况和饼干,<一的href="http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientCustomContext.java">complete code这里

Example Use of context and cookies, complete code here

    HttpClient httpclient = new DefaultHttpClient();

    // 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/", localContext);

这篇关于Android的HttpClient的永久性Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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