2016-01-12 102 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。

相關問題