Android: HttpClient与Webview共享cookies

邓仲卿
2023-12-01

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


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();
	}
}


 类似资料: