Android: HttpClient与Webview共享cookies

本人最近和别人合做一个app应用,app加webview。
app登陆有webview也要是登陆状态,解决的办法是app登陆后把设置下cookie

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

猜你喜欢

转载自flyvszhb.iteye.com/blog/2154921