【问题标题】:: Persistent cookies used in HttpClient and HttpGet access:在 HttpClient 和 HttpGet 访问中使用的持久 cookie
【发布时间】:2014-04-07 17:47:46
【问题描述】:

我希望用户通过 HttpGet/HttpPost 操作执行不同的任务。 我想保存 Cookie,以便用户只需要在 cookie 到期后登录。

在浏览互联网时,我发现“PersistentCookiestore”是一个很好的起点。

问题1:如何在HttpClient软件中使用(apache)PersistentCookieStore?我没有看到完整的例子,例如如何在第一次使用 httpclient 时开始使用 PersistentCookieStore。

参见示例:

static PersistentCookieStore cookie_jar = new PersistentCookieStore( getApplicationContext()); 

public void login() { 
    // how to connect the persistent cookie store to the HttpClient? 
    ....
    client2 = new DefaultHttpClient( httpParameters);
    …
    client2.setCookieStore(cookie_jar);
    ....
    HttpGet method2 = new HttpGet(uri2);
    ....
    try {
     res = client2.execute(method2); 
}
catch ( ClientProtocolException e1) { e1.printStackTrace(); return false; }
    ....

问题 2:如何在通话后更新 cookie,或者这永远不需要? 换句话说:当我在调用 client2.execute(...) 后调用 HttpGet 或 HttpPost 后必须更新 cookie 时。

在我看到的带有 httpclient 的(非持久性)cookie 的示例代码中:

cookie_jar = client.getCookieStore(); 
….
HttpGet or HttpPost … 
client.setCookieStore( ....) 
client.execute( .. )  // second call

感谢您的帮助。

【问题讨论】:

    标签: android cookies androidhttpclient cookiestore


    【解决方案1】:

    问题 1:我将 AsyncHttpClient 与 PersistenCookieStore 一起使用

    问题 2:我只在用户登录应用时更新我的​​ cookie

    AsyncHttpClient client = new AsyncHttpClient();
    PersistentCookieStore myCookie = new PersistentCookieStore(CONTEXT);
    
    
    //Clean cookies when LOGIN
    if(IS_LOGIN)
        myCookie.clear();
    
    client.setCookieStore(myCookie);
    

    现在我想知道如何知道 cookie 何时过期

    【讨论】:

    • 感谢您的回复……经过多次尝试,我选择了上面的那个。
    【解决方案2】:

    最初的问题是:如何在应用启动后防止每次登录?答案当然是使用cookies。那么如何存储 cookie 以便在应用重启后重复使用?

    对我有用的答案非常简单:只需将 cookiestore 转换为字符串,在退出应用程序之前将其放入共享首选项中。 在下一个应用程序启动之后,因此在下一次登录之前,只需从共享首选项中获取刺痛,将其转换回 cookiestore。使用 cookiestore 会阻止我再次登录。

    public void saveCookieStore( CookieStore cookieStore) {
        StringBuilder cookies = new StringBuilder();
        for (final Cookie cookie : cookieStore.getCookies()) {
            cookies.append(cookie.getName());
            cookies.append('=');
            cookies.append(cookie.getValue());
            cookies.append('=');
            cookies.append(cookie.getDomain());
            cookies.append(';');
        }
        SharedPreferences.Editor edit = sharedPref.edit();
        edit.putString( MY_GC_COOKIESTORE, cookies.toString());
        edit.commit();
    }
    
    // retrieve the saved cookiestore from the shared pref
    public CookieStore restoreCookieStore() {
        String savedCookieString = sharedPref.getString( MY_GC_COOKIESTORE, null);
        if( savedCookieString == null || savedCookieString.isEmpty()) { 
            return null; 
        }
        CookieStore cookieStore = new BasicCookieStore();
        for ( String cookie : StringUtils.split( savedCookieString, ';')) {
            String[] split = StringUtils.split( cookie, "=", 3);
            if( split.length == 3) {
                BasicClientCookie newCookie = new BasicClientCookie( split[0], split[1]);
                newCookie.setDomain( split[2]);
                cookieStore.addCookie( newCookie);
            }
        }
        return cookieStore;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多