使用 HttpClient 时跨活动使用 Cookie

2022-09-01 17:22:20

我正在尝试一个简单的应用程序来阅读网站的HTML,并将其转换以在Android中创建一个易于阅读的UI(这是学习Android的exersize,而不是制作一个可用的应用程序)。我遇到的问题是跨活动持久保存用户会话,然后在一旦召回时使用该会话。HttpClient

我想“正确”地执行此操作,推荐的方法似乎是使用.然而,我遇到了问题 - 我似乎找不到“正确”的方式从 中获取a并在以后的单独活动中实例化使用它。CookieManagerCookieCookieManagerHttpClient

使用时,我可以保存 和 然后 在其他活动的作用域中(请参阅代码片段 2)。我以后在请求页面时没有找到如何使用它(请参阅代码片段3)。CookieManagerCookieCookie

说得够多了,这里有一些代码。首先,我的登录操作和 Cookie 存储:

private OnClickListener loginActionListener = new OnClickListener() 
{
    public void onClick(View v) 
    {
        EditText usernameTextView = (EditText) findViewById(R.id.Username);
        EditText passwordTextView = (EditText) findViewById(R.id.Password);
        String username = usernameTextView.getText().toString();
        String password = passwordTextView.getText().toString();

        try {
            HttpPost postMethod = new HttpPost(URI);                
            HttpParams params   = new BasicHttpParams();

            params.setParameter("mode", "login");
            params.setParameter("autologin", true);
            params.setParameter("username", username);
            params.setParameter("password", password);
            postMethod.setParams(params);

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpResponse response        = httpClient.execute(postMethod);
            List<Cookie> cookies = httpClient.getCookieStore().getCookies();

            if(cookies != null)
            {
                for(Cookie cookie : cookies)
                {
                    String cookieString = cookie.getName() + "=" + cookie.getValue() + "; domain=" + cookie.getDomain();                        
                    CookieManager.getInstance().setCookie(cookie.getDomain(), cookieString);  
                }
            }
            CookieSyncManager.getInstance().sync();

            Intent intent = new Intent(v.getContext(), IndexAction.class);
            startActivity(intent);
    } catch (Exception e) {...}
}

决定是让用户登录还是转到索引的启动活动如下。从此代码中可以看出,Cookie 在范围内,可以读取:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    CookieSyncManager.createInstance(this);

    if(CookieManager.getInstance().getCookie(URI) == null)
    {
        Intent intent = new Intent(this, LoginAction.class);
        startActivity(intent);
    }
    else
    {
        Intent intent = new Intent(this, IndexAction.class);
        startActivity(intent);
    }
}

但是从我的代码中阅读索引页面,我希望你能建议我错过了什么:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    CookieSyncManager.createInstance(this);

    try
    {
            HttpGet getMethod = new HttpGet(URI_INDEX);  

            HttpParams params   = new BasicHttpParams();                        
            HttpConnectionParams.setConnectionTimeout(params, 30000);
            HttpConnectionParams.setSoTimeout(params, 30000);

            // This code results in a ClassCastException, I'm assuming i've found a red herring with this solution.
            // HttpContext localContext = new BasicHttpContext();    
            // localContext.setAttribute(ClientContext.COOKIE_STORE, CookieManager.getInstance().getCookie(URI));

            DefaultHttpClient httpClient = new DefaultHttpClient(params);
            HttpResponse response        = httpClient.execute(getMethod);

            if(response.getStatusLine().getStatusCode() > 299 && response.getStatusLine().getStatusCode() < 400)
            {
                // Not logged in doesn't give a redirect response. Very annoying.
            }

            final char[] buffer = new char[0x10000];
            StringBuilder out = new StringBuilder();
            Reader in = new InputStreamReader(response.getEntity().getContent(), "UTF-8");
            int read = 0;
            while (read>=0)
            {
              read = in.read(buffer, 0, buffer.length);
              if (read>0) {
                out.append(buffer, 0, read);
              }
            }

            String returnString = out.toString();
    } catch (ClientProtocolException e) {...}
}

on 没有使用 Cookie(在调试中仔细检查了这一点)来拉回页面。如果有人能填补我知识中的这个漏洞,那就太好了。HttpClientexecute(getMethod)

提前致谢。

编辑

当注释的代码被添加回去(将方法更改为 )时,将生成此跟踪跟踪 - 假设是因为我正在用 a 而不是 :httpClient.execute(getMethod)httpClient.execute(getMethod, localContext)ClientContext.COOKIE_STORECookieStringCookieStore

*org.apache.http.client.protocol.RequestAddCookies.process(RequestAddCookies.java:88), org.apache.http.protocol.BasicHttpProcessor.process(BasicHttpProcessor.java:290), org.apache.http.protocol.HttpRequestExecutor.preProcess(HttpRequestExecutor.java:160), org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:401)
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555), org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487), 
com.testapp.site.name.IndexAction.onCreate(IndexAction.java:47), 
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047), 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611), 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663), 
android.app.ActivityThread.access$1500(ActivityThread.java:117), 
android.app.ActivityThread$H.handleMessage(ActivityThread.java:931), 
android.os.Handler.dispatchMessage(Handler.java:99), 
android.os.Looper.loop(Looper.java:123), 
android.app.ActivityThread.main(ActivityThread.java:3683), 
java.lang.reflect.Method.invokeNative(Native Method), 
java.lang.reflect.Method.invoke(Method.java:507), 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839), 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597), 
dalvik.system.NativeStart.main(Native Method)*

答案 1

CookieManager由 Java 的内部 HTTP 客户端使用。它与Apache HttpClient无关。

在您的代码中,您始终为每个请求创建一个新实例,从而创建一个新实例,一旦该实例超出范围,显然就会与存储在中的所有cookie一起收集垃圾。HttpClientCookieStoreHttpClient

您应该

(1)对所有逻辑相关的HTTP请求重用相同的HttpClient实例,并在所有逻辑相关的线程之间共享它(这是使用Apache HttpClient的推荐方式)

(2)或者至少在逻辑相关的线程之间共享相同的CookieStore实例

(3)或者,如果您坚持使用来存储所有cookie,请创建一个由CookieManager支持的自定义CookieStore实现CookieManager


答案 2

(正如所承诺的那样,解决方案。我仍然不喜欢它,觉得我错过了“正确”的方式,但是,它有效。

您可以使用 通过以下代码注册您的 Cookie(因此使这些 Cookie 在应用程序之间可用):CookieManager

将饼干保存到 :CookieManager

List<Cookie> cookies = httpClient.getCookieStore().getCookies();

if(cookies != null)
{
    for(Cookie cookie : cookies)
    {
        String cookieString = cookie.getName() + "=" + cookie.getValue() + "; domain=" + cookie.getDomain();                        
        CookieManager.getInstance().setCookie(cookie.getDomain(), cookieString);  
    }
}
CookieSyncManager.getInstance().sync();

检查指定域上的 Cookie:if(CookieManager.getInstance().getCookie(URI_FOR_DOMAIN)

要为 HttpClient 重新构造值,请执行以下操作:

DefaultHttpClient httpClient = new DefaultHttpClient(params);
String[] keyValueSets = CookieManager.getInstance().getCookie(URI_FOR_DOMAIN).split(";");
for(String cookie : keyValueSets)
{
    String[] keyValue = cookie.split("=");
    String key = keyValue[0];
    String value = "";
    if(keyValue.length>1) value = keyValue[1];
    httpClient.getCookieStore().addCookie(new BasicClientCookie(key, value));
}

推荐