Android 只有第一个cookie进入服务器,其他几个cookie不';T
我想通过cookie传递Android 只有第一个cookie进入服务器,其他几个cookie不';T,android,cookies,android-webview,android-cookiemanager,Android,Cookies,Android Webview,Android Cookiemanager,我想通过cookie传递apuid和apitoken。但我只得到服务器上的第一个。 这是我初始化cookie的代码: public static void initCookie(String uid,String token,String domain,Context context){ try{ CookieSyncManager.createInstance(context); CookieManager cookieManag
apuid
和apitoken
。但我只得到服务器上的第一个。
这是我初始化cookie的代码:
public static void initCookie(String uid,String token,String domain,Context context){
try{
CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.removeSessionCookie();
cookieManager.removeAllCookie();
cookieManager.setCookie(domain,"apiuid="+uid +";apitoken="+token);
CookieSyncManager.getInstance().sync();
}catch(Throwable e){
LogUtils.e(e);
}
}
阅读代码后:
/**
* Sets a cookie for the given URL. Any existing cookie with the same host,
* path and name will be replaced with the new cookie. The cookie being set
* will be ignored if it is expired.
*
* @param url the URL for which the cookie is to be set
* @param value the cookie as a string, using the format of the 'Set-Cookie'
* HTTP response header
*/
public abstract void setCookie(String url, String value);
/**
* Gets the cookies for the given URL.
*
* @param url the URL for which the cookies are requested
* @return value the cookies as a string, using the format of the 'Cookie'
* HTTP request header
*/
public abstract String getCookie(String url);
我注意到一个是“设置Cookie”,另一个是“Cookie”。
所以,我想我可以逐一添加
最终代码如下:
public static void initCookie(String uid,String token,String domain,Context context){
try{
CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.removeSessionCookie();
cookieManager.removeAllCookie();
cookieManager.setCookie(domain,"apiuid="+uid);
cookieManager.setCookie(domain,"apitoken="+token);
CookieSyncManager.getInstance().sync();
}catch(Throwable e){
LogUtils.e(e);
}
}
以下是我不太确定的事情:
根据Mark():
请注意:此代码已严重损坏!Android CookieManager返回根据RFC2109第4.3.4节格式化的cookie列表,该列表由分号或逗号分隔。然而,在Android的实现中,它们返回分号分隔的结果。RFC2109Spec对象希望它们用逗号分隔。因此,如果某个特定域有多个cookie,则此代码将中断,并仅传输列表中的第一个cookie