在我的服务中,我运行此代码:
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() {
...
}
...
...
}
当我查看我的网络服务器日志时,我可以看到请求标头中存在cookie 但这些饼干都是旧饼干。
我还从Activity运行此AndroidAsyncHttp代码。发送相同的旧cookie。
但是当我在WebView中打印出当前的cookie时,我看到了新的cookie。
如何使用AndroidAsyncHttp发送WebView的cookie?
答案 0 :(得分:2)
在设置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() {
...
}