Android的web视图自动登录到HTTPS网站通过设置标记饼干 [英] Android Webview auto login to https website by setting token cookie
问题描述
我试图做到的,是通过设置令牌作为一个cookie来自动登录到一个HTTPS的网站。
What I am trying to achieve is to autologin to a https website by setting a token as a cookie.
(它适用于Android的Chrome浏览器,但不是在应用程序的WebView)
( It works on android chrome browser but not in application webview )
基本上我面临两个问题,同时装载HTTPS URL转换为Web视图的cookie设置
Basically I am facing two issue while loading https url into web view with cookie set
第1
我收到以下日志消息。
无法验证证书链,错误: java.security.cert.CertPathValidatorException:为信任锚 找不到证书路径。
Failed to validate the certificate chain, error: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
我试图重写 onReceivedSslError
键,名为 handler.proceed();
如下
I tried overriding onReceivedSslError
and called handler.proceed();
as below.
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
Log.d(TAG, "==> " + error.toString());
handler.proceed();
}
但我仍看到的白页(我假设它的发生,因为证书的问题。)
But still I see white page ( I am assuming Its happening because of certificate issue. )
第2期
我有我的登录网址(如 https://www.abc.com/login。 HTML )
I have got a login url with me ( e.g https://www.abc.com/login.html )
我试图实现自动登录到Web视图通过设置一个cookie。
What I am trying to achieve is auto login into web view by setting a cookie.
CookieSyncManager.createInstance(webView.getContext());
CookieManager cookieManager = CookieManager.getInstance();
CookieManager.getInstance().setAcceptCookie(true);
String token = PreferenceHelper.loadTokenFromPreference(this);
String sessionCookie = "staging=" + token;
cookieManager.setCookie("https://www.abc.com/aaa/",
sessionCookie);
CookieSyncManager.getInstance().sync();
SystemClock.sleep(1000);
但我仍是不能够自动login.Rather我看到白色的页面。
But still I am not able to auto login.Rather I am seeing the white page.
什么我不知道现在究竟在何处,我犯了一个错误。
What I am not sure now is exactly where I am making a mistake.
cookieManager.setCookie
需要第一个参数是一个url为其cookie的需要设置,我不知道究竟我需要给它哪个网址?
cookieManager.setCookie
requires first argument as a url for which cookie needs to set,I am not sure exactly which url i need to give it ?
任何人都可以建议我正确的方法来得到它的工作?
Can anyone suggest me the right approach to get it working ?
感谢
推荐答案
搜寻了好一阵子之后,嗯,我终于得到了解决。
Well after searching for quite a while I finally got the solution.
我不得不添加下面几行,以得到它的工作。
I had to add following lines to get it working.
webView.getSettings().setAppCachePath(appCachePath);
webView.getSettings().setAppCacheEnabled(true);
这篇关于Android的web视图自动登录到HTTPS网站通过设置标记饼干的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!