您的位置:首页 > 理论基础 > 计算机网络

Android: HttpClient与Webview共享cookies

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

[java] view
plaincopy

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

    }  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: