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

    }
发布了24 篇原创文章 · 获赞 5 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/g707175425/article/details/46006975
今日推荐