2011-02-17 48 views
2

當我的應用程序啓動時,我會向Web服務發佈登錄請求並接收Cookie。我想使用的Cookie在我的WebView中,我用下面的代碼做:在Android中是否有一個用於將Cookie格式化爲CookieManager值的類?

List<Cookie> cookies = this.get_my_cookies_from_somewhere(); 
CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(this); 
CookieManager cookieManager = CookieManager.getInstance(); 
cookieManager.removeAllCookie(); 

for (Cookie cookie : cookies) { 
    String rawUrl = (cookie.isSecure() ? "https" : "http") + "://" + cookie.getDomain() + cookie.getPath(); 
    cookieManager.setCookie(rawUrl, cookie.getName() + "=" + cookie.getValue() + "; domain=" + cookie.getDomain()); 
} 
cookieSyncManager.sync(); 

理想情況下,我想有cookie的url和「設置Cookie」由CookieManager所需的頭值由建Cookie類或一些實用程序類。這樣的事情存在嗎?

我試過RFC2109Spec和RFC2965Spec,但它們產生「Cookie」頭,而不是「Set-Cookie」頭。

回答

0

首選的方法似乎是將響應中的標題直接發送到CookieManager。我在此基礎上code I found from Romain Guy

final CookieManager cookieManager = CookieManager.getInstance(); 
final HttpResponse response = ... 
final Header[] cookies = response.getHeaders("set-cookie"); 
for (Header cooky : cookies) { 
    cookieManager.setCookie(url, cooky.getValue()); 
} 
相關問題