0

ユーザーに 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

助けてくれてありがとう。

4

2 に答える 2

0

元の質問は、アプリの起動後に毎回ログインできないようにするにはどうすればよいですか? 答えはもちろんクッキーを使用することです。では、アプリの再起動後に再利用できるように、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;
}
于 2014-12-22T07:59:05.950 に答える
0

質問 1: PersistenCookieStore で AsyncHttpClient を使用しています

質問 2:ユーザーがアプリにログインしたときのみ Cookie を更新します

AsyncHttpClient client = new AsyncHttpClient();
PersistentCookieStore myCookie = new PersistentCookieStore(CONTEXT);


//Clean cookies when LOGIN
if(IS_LOGIN)
    myCookie.clear();

client.setCookieStore(myCookie);

現在、Cookieの有効期限がいつ切れたかを知る方法を知りたいです

于 2014-11-03T14:49:00.067 に答える