WebView 不接受某些 cookie
WebView not accepting some cookies
我正在 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;
}
使用 Chrome 开发者工具,我可以看到网站的以下 cookie
当我调用方法 CookieManager.getInstance().getCookie(url)
时,我可以看到 cookie JESESSIONID 和 SLB_Cookie 存在于webview,但不确定本地存储 cookie。此外,如果我在 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 传递您的登录对象。希望对你有帮助。
我正在 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;
}
使用 Chrome 开发者工具,我可以看到网站的以下 cookie
当我调用方法 CookieManager.getInstance().getCookie(url)
时,我可以看到 cookie JESESSIONID 和 SLB_Cookie 存在于webview,但不确定本地存储 cookie。此外,如果我在 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 传递您的登录对象。希望对你有帮助。