在我的服务中运行以下代码:
public class MainService extends Service {
....
....
CookieManager mCookieManager = CookieManager.getInstance();
CookieSyncManager mCookieSyncManager = CookieSyncManager.createInstance(mContext);
if (mCookieSyncManager != null) {
mCookieSyncManager.sync();
}
AsyncHttpClient myClient = new AsyncHttpClient();
PersistentCookieStore myCookieStore = new PersistentCookieStore(mContext);
myClient.setCookieStore(myCookieStore);
myClient.setUserAgent("my service");
myClient.get("http://example.com/mypage/", new AsyncHttpResponseHandler() {
...
}
...
...
}
当我检查我的web服务器日志时,我可以看到cookie存在于请求头中。
但是这些饼干是旧饼干。
我也运行这个AndroidAsyncHttp代码从一个活动。发送相同的旧cookie
但是当我在WebView中打印当前的cookie时,我看到了新的cookie。
如何发送WebView的cookie与AndroidAsyncHttp ?
设置cookie前清除cookie
myCookieStore.clear();
根据我的经验,我不需要CookieManager。我只使用这个
AsyncHttpClient myClient = new AsyncHttpClient();
PersistentCookieStore myCookieStore = new PersistentCookieStore(mContext);
// clear cookie to make the fresh cookie, to ensure the newest cookie is being send
myCookieStore.clear();
// set the new cookie
myClient.setCookieStore(myCookieStore);
myClient.get("http://example.com/mypage/", new AsyncHttpResponseHandler() {
...
}