我正在 Webview 中加载一个网站,该网站使用一些 cookie 来存储会话。我写了以下几行来接受cookie
CookieSyncManager.createInstance(this);
CookieSyncManager.getInstance().startSync();
CookieManager.getInstance().setAcceptCookie(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
CookieManager.setAcceptFileSchemeCookies(true);
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
}
webView.loadUrl("https://www.irctc.co.in/nget/train-search");
在某一阶段(从支付网关付款后),shouldOverrideUrlLoading
方法被调用,之后它应该登陆到交易成功页面,但它继续进入登录页面。这是我的方法:
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
Log.d(TAG, "The URL is loaded in webview itself");
return false;
}
Using Chrome Developer Tools, I can see following cookies for website
当我调用方法时CookieManager.getInstance().getCookie(url)
我可以看到饼干杰西森ID and SLB_Cookie存在于 webview 中,但不确定本地存储曲奇饼。另外,如果我删除本地存储Chrome 中的 cookie,我得到完全相同的页面(登录页面),而不是像 WebView 这样的交易成功页面。所以我认为,如果我以任何方式检查本地存储 cookie 是否存在于 webview 中,如果不存在,那么如果我添加它,我的工作就完成了。但我无法完成这两项任务。
如果您想将登录用户数据传递到 Webview 中的 cookie,请这样做
前几天我必须将登录对象传递到特定的 URL
如下。
WebSettings settings = webViewRoi.getSettings();
settings.setDomStorageEnabled(true);
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(false);
settings.setAppCacheEnabled(true);
settings.setAllowUniversalAccessFromFileURLs(true);
settings.setAllowFileAccessFromFileURLs(true);
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
FormsDTO formsDTO = new FormsDTO();
FormsDTOProfile dtoProfile = new FormsDTOProfile(LoginActivity.loginInfoDTO.getProfile());
formsDTO.setProfile(dtoProfile);
formsDTO.setAuthorized(true);
formsDTO.setToken(LoginActivity.loginInfoDTO.getToken());
String out123 = gson.toJson(formsDTO);
String auth2 = URLEncoder.encode(out123, "UTF-8");
String z = "userInfo=" + auth2; // here userinfo is the key of cookie
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.setCookie(url, z);
webViewRoi.setWebChromeClient(new WebChromeClient());
webViewRoi.loadUrl(url);
因此,如果您知道 cookie 的键名,那么您可以通过 cookie 传递您的登录对象。希望对你有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)