Android HttpClient Cookie 保持登录



保存cookie

List cookies = httpClient.getCookieStore().getCookies();

if (cookies != null) {

for (Cookie cookie : cookies) {

String cookieString = cookie.getName() + "=" + cookie.getValue() + "; domain=" + cookie.getDomain();                        

       CookieManager.getInstance().setCookie(cookie.getDomain(), cookieString);  

}

}

CookieSyncManager.getInstance().sync();


使用cookie

                String[] keyValueSets = CookieManager.getInstance().getCookie(domain).split(";");

for(String cookie : keyValueSets) {

   String[] keyValue = cookie.split("=");

   String key = keyValue[0];

   String value = "";

   if(keyValue.length>1) value = keyValue[1];

   BasicClientCookie baseCookie = new BasicClientCookie(key, value);

   baseCookie.setDomain(domain);

   httpClient.getCookieStore().addCookie(baseCookie);

}



转载于:https://my.oschina.net/mars/blog/398974

你可能感兴趣的:(Android HttpClient Cookie 保持登录)