WebView不接受某些Cookie [英] WebView not accepting some cookies

查看:213
本文介绍了WebView不接受某些Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Webview中加载一个网站,该网站使用一些cookie来存储会话.我已经写了以下几行来接受Cookie

I'm loading a website in Webview which uses some cookies to store session. I've written following lines to accept cookies

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方法,此后应该进入交易成功页面,但仍会继续登录页面.这是我的方法:

At one stage (after payment from payment gateway), shouldOverrideUrlLoading method is called and after that it's supposed to land to transaction success page but it keeps going to login page. This is my method:

@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
    Log.d(TAG, "The URL is loaded in webview itself");
    return false;
}

使用Chrome开发者工具,我可以看到以下网站的Cookie

Using Chrome Developer Tools, I can see following cookies for website

调用方法CookieManager.getInstance().getCookie(url)时,我可以看到Web视图中存在cookie JESESSIONID SLB_Cookie ,但是不确定本地存储 cookie.另外,如果我在Chrome中删除本地存储 Cookie,则会得到完全相同的页面(登录页面),而不是WebView之类的交易成功页面.因此,我认为无论如何如果我要检查本地存储cookie是否存在于webview中,如果不存在,那么如果我添加它,我的工作就可以完成.但是我无法完成任何一项任务.

When I call method CookieManager.getInstance().getCookie(url) I can see that the cookies JESESSIONID and SLB_Cookie are present in webview, but not sure about that Local storage cookie. Also, if I remove the Local storage cookie in Chrome, I'm getting the exact same page (login page) instead of transaction success page like WebView. So I think if by any means if I check whether the Local Storage cookie is present in webview or not, and if not present then if I add it, my job would be done. But I'm not able to achieve either of the task.

推荐答案

如果要将登录用户数据传递到Webview中的cookie,请按照以下步骤进行操作 前几天,我不得不将登录对象传递给特定的URL 如下.

If you want to pass your login users data to a cookie in Webview the do it like this The other day I had to pass my login object to particular URL as follow.

            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传递登录对象.希望对您有帮助.

So if you know the key name of cookie then you can pass your login object through cookie. hope it helps you.

这篇关于WebView不接受某些Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆