WebView添加手动拼接Cookie

同样使用CookieManager
/** * 同步一下cookie */
    public static void synCookies(Context context, String url) {
        CookieSyncManager.createInstance(context);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
        cookieManager.removeAllCookie();//移除
        /**cookie*/

        String expires = new Date(System.currentTimeMillis() + 86400000).toGMTString();
        String host = Uri.parse(url).getHost().replace("www", "");

        String uidCookie = "uid=\""+GlobalVariable.getCurrentUserID()+"\";$Domain=\"" + host + "\";$Path=\"" + NetConst.URL_COMPANY_FLOWERCHART_COOKIE_PATH +
                "\";Max-Age=\"86400\";expires=\""+expires;//+"\";Secure;";
        String cidCookie = "cid=\""+GlobalVariable.getCurrentSelectedCompanyID()+"\";$Domain=\"" + host + "\";$Path=\"" + NetConst.URL_COMPANY_FLOWERCHART_COOKIE_PATH +
                "\";Max-Age=\"86400\";expires=\""+expires;//+"\";Secure;";

        /**cookie*/
        cookieManager.setCookie(url,uidCookie);
        cookieManager.setCookie(url,cidCookie);
        CookieSyncManager.getInstance().sync();

    }

你可能感兴趣的:(cookie,webView)