Android Webview通过设置令牌cookie自动登录到https网站 [英] Android Webview auto login to https website by setting token cookie

查看:1685
本文介绍了Android Webview通过设置令牌cookie自动登录到https网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要实现的是通过将令牌设置为cookie来自动登录到https网站。



(它适用于Android Chrome浏览器,但不适用于应用程序web view)



基本上,我遇到了两个问题,即将https网址加载到网络视图中并设置了Cookie



第1期



我收到以下日志消息。


无法验证证书链,错误:
java.security.cert.CertPathValidatorException:未找到
证书路径的信任锚点


我尝试覆盖 onReceivedSslError 并调用 handler.proceed(); p>

  @Override 
public void onReceivedSslError(WebView视图,SslErrorHandler处理程序,SslError错误){
Log.d TAG,==>+ error.toString());
handler.proceed();
}

但是我仍然看到白页。)



第2期



我有一个登入网址例如 https://www.abc.com/login.html



我想实现的是通过设置cookie来自动登录到web视图。

  CookieSyncManager.createInstance .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);

但是我还是无法自动登录。我看到白页。



我现在不知道的正是我犯错误的地方。



cookieManager。 setCookie 需要第一个参数作为需要设置Cookie的网址,我不知道我需要给哪个网址。



解决方案



我必须添加以下行才能使其正常工作。

  webView.getSettings()。setAppCachePath(appCachePath); 
webView.getSettings()。setAppCacheEnabled(true);


What I am trying to achieve is to autologin to a https website by setting a token as a cookie.

( It works on android chrome browser but not in application webview )

Basically I am facing two issue while loading https url into web view with cookie set

Issue 1

I am getting following log message.

Failed to validate the certificate chain, error: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

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. )

Issue 2

I have got a login url with me ( e.g https://www.abc.com/login.html )

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);

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 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 ?

Thanks

解决方案

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 Webview通过设置令牌cookie自动登录到https网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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