2016-01-12 100 views
-1

我想通过cookie来传递apiuidapitoken。但我只能得到服务器上的第一个。 这是我的代码初始化饼干:只有第一个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 +";apitoken="+token); 
      CookieSyncManager.getInstance().sync(); 
     }catch(Throwable e){ 
      LogUtils.e(e); 
     } 
    } 

回答

0

阅读代码之后:

/** 
* 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”,另一个是“曲奇”。 所以,我想我可以一个接一个地加。

最终代码如下:

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(http://blog.winfieldpeterson.com/2013/01/17/cookies-in-hybrid-android-apps/):

请注意:此代码是严重破! Android CookieManager根据RFC2109第4.3.4节:http://tools.ietf.org/html/rfc2109#section-4.3.4给出了一个cookie列表,该列表使用分号或逗号分隔。但是,在Android的实现中,它们返回分号分隔符。 RFC2109Spec对象期望它们由逗号分隔。因此,如果某个特定域有多个Cookie,则此代码将中断,并且仅传输列表中的第一个Cookie。

相关问题