Android Cookie 解析并且设置给WebView 保持登陆状态

     公司用到一个功能就是根据登录接口返回cookie,保存cookie,用网页打开保持登陆状态。
     流程如下:1、获取Cookie 2、设置Cookie
                HttpClient httpClient = new DefaultHttpClient();
		String loginUrl = "";//你的登录url
		HttpPost httpPost = new HttpPost(loginUrl);
		CookieStore cookieStore = null;
		
		try {
			HttpResponse response = httpClient.execute(httpPost);
			cookieStore = ((AbstractHttpClient) httpClient).getCookieStore();
			List<Cookie> cookies = cookieStore.getCookies();
			for (int i = 0; i < cookies.size(); i++) {
				Cookie cookie = cookies.get(i);
				synCookies(this,cookie.getDomain(), cookie.getName() + "=" + cookie.getValue());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		String pageUrl = "";//你要用WebView打开的url
		webView.loadUrl(pageUrl);
 
                
	public void synCookies(Context context, String url, String cookies) {
		
		CookieSyncManager.createInstance(context);
		CookieManager cookieManager = CookieManager.getInstance();
		cookieManager.setAcceptCookie(true);
		cookieManager.removeSessionCookie();// 移除
		cookieManager.setCookie(url, cookies);// 指定要修改的cookies
		CookieSyncManager.getInstance().sync();
		
	}
 

                 邪恶漫画下载

    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值