美文网首页
【WebView】同步Cookies

【WebView】同步Cookies

作者: jianshuwangyd | 来源:发表于2019-05-17 00:21 被阅读0次
  1. 先同步再loadUrl
     syncCookies() 
     mWebView.loadUrl(url);
  1. 同步方法
    /**
     * 同步Cookies
     */
    private void syncCookies() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            CookieSyncManager.createInstance(this);
        }

        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            cookieManager.removeSessionCookie();
        } else {
            cookieManager.removeSessionCookies(null);
        }

        String baseUrl = Constant.getBaseUrl();
        String host = Uri.parse(baseUrl).getHost();
        Set<String> cookies = CookiesUtils.getInstance().getCookie(baseUrl, host);
        for (String cookie : cookies) {
            cookieManager.setCookie(host, cookie);
        }

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            CookieSyncManager.getInstance().sync();
        } else {
            cookieManager.flush();
        }
    }
  1. cookies获取方式
 response.headers("Set-Cookie");

相关文章

网友评论

      本文标题:【WebView】同步Cookies

      本文链接:https://www.haomeiwen.com/subject/zgegaqtx.html