我希望用户通过 HttpGet/HttpPost 操作执行不同的任务。我想保存 Cookie,以便用户只需要在 cookie 到期后登录。
在浏览互联网时,我看到“PersistentCookiestore”是一个很好的起点。
问题一:如何在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
谢谢你的帮忙。