在WebView中启用CookieManager,并在每次加载新页面时设置cookies。
代码示例:
// 启用cookie管理器 CookieManager cookieManager = CookieManager.getInstance(); cookieManager.setAcceptCookie(true);
// 设置cookies,可以在登录后设置 cookieManager.setCookie("example.com", "cookieName=cookieValue");
// 加载网页时设置cookies webView.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); String cookies = cookieManager.getCookie(url); if (cookies != null) { view.loadUrl("javascript:document.cookie="" + cookies + """); } } });