java Android HttpClient 持久性 cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4146861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:01:38  来源:igfitidea点击:

Android HttpClient persistent cookies

javaandroidcookieshttpclient

提问by Knossos

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 Retrofitand 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 不再推荐 HttpClient (阅读:已弃用),而是推荐 HttpUrlConnection。现在使用的库的一个很好的例子是RetrofitOkHttp。在这个问题的上下文中,cookie 可以保存、存储和交付与后续查询。这不是透明处理的。使用OkHttp,您可以使用 Interceptors

I have an Android application with multiple intents.

我有一个具有多个意图的 Android 应用程序。

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

第一个意图是登录表单,后续意图依赖于登录过程提供的 cookie。

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).

我遇到的问题是 cookie 似乎没有在意图中持续存在。我正在每个意图中创建新的 HttpClients(我最初尝试将 Parcelable 传输到每个意图,但效果不佳)。

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

有没有人有任何使 cookie 跨意图持久化的技巧?

采纳答案by Aaron Saunders

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

您可以按照@Emmanuel 的建议进行操作,也可以在您创建的 HttpClient 之间传递BasicHttpContext

Example Use of context and cookies, complete code here

上下文和cookies的使用示例,完整代码在这里

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

回答by Emmanuel

Don't create new HttpClients; this will clear the cookies. Reuse a single HttpClient.

不要创建新的 HttpClient;这将清除 cookie。重用单个 HttpClient。

回答by user569873

Make your httpClient a singleton class.

使您的 httpClient 成为单例类。

回答by panicstyle

define HttpClient in Application class, and use it in activity.

在 Application 类中定义 HttpClient,并在活动中使用它。

in Application

在申请中

public class AAA extends Application {
    public HttpClient httpClient; 

    httpClient = new DefaultHttpClient(); 

in Activity

在活动中

AAA aaa = (AAA)getApplication();
httpClient = app.httpClient;