Android 应用内HttpClient 与 WebView 共享 Cookie

DefaultHttpClient httpclient=....;
String toUrl="https://cap.cityu.edu.hk/studentlan/details.aspx.....";

List<Cookie> cookies = httpclient.getCookieStore().getCookies();

if (!cookies.isEmpty()){
	CookieSyncManager.createInstance(this);
	CookieManager cookieManager = CookieManager.getInstance();
        //sync all the cookies in the httpclient with the webview by generating cookie string
	for (Cookie cookie : cookies){
		String cookieString = cookie.getName() + "=" + cookie.getValue() + "; domain=" + cookie.getDomain();
		cookieManager.setCookie(toUrl, cookieString);
		CookieSyncManager.getInstance().sync();
	}
}

httpclient 与 webview需要进行cookie 共享,因为如果不共享,那么假设你在httpclient进行了登录,然后用webview里打开那些login之后才能看的page,就会叫你再login;

一般我会把 httpclient 设置为静态的;

转载请注明出处:http://duwei.cnblogs.com/
原文地址:https://www.cnblogs.com/duwei/p/3896835.html