jam*_*esc 16 android android-webview
我已经研究和研究过这个问题,直到我变得灰白秃顶.我如何得到一个webview,适用于需要通过api级别8+上的https连接进行http基本身份验证的站点
我有以下代码
String email = Util.getEmail(this);
String pwd = Util.getPassword(this);
webview.getSettings().setJavaScriptEnabled(true);
webview.setHttpAuthUsernamePassword(Config.SERVER_BASE_URL, "Application", email, pwd);
// webview.setWebViewClient(new MobileWebViewClient(this));
webview.loadUrl(url);
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我确实有一个Web视图客户端(现已注释掉)覆盖了onReceivedHttpAuthRequest方法,该方法看起来像这样
@Override
public void onReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, String host, String realm){
String email = Util.getEmail(wvContext);
String pwd = Util.getPassword(wvContext);
if(!pwd.equalsIgnoreCase("-1") && !pwd.equalsIgnoreCase("-1")){
handler.proceed(email, pwd);
}
}
Run Code Online (Sandbox Code Playgroud)
这是在没有webview.setHttpAuthUsernamePassword的情况下使用并且工作正常,除了它意味着向网站发出了2个请求 - 第一个获得401然后客户端使用授权内容启动这对于少量网站流量很好但是减半流量(目前平均49个请求p/m)是现在的游戏名称!
我读到我可以通过使用先发制人地提供凭据
webview.setHttpAuthUsernamePassword(Config.SERVER_BASE_URL, "Application", email, pwd);
Run Code Online (Sandbox Code Playgroud)
然而,这只会导致Http Basic:访问被拒绝错误服务器基本URL常量是站点的域名,即https://example.com(没有页面)实际URL是https://example.com/some_pages.无论是使用完整网址还是域名都没有区别.我检查了领域,我有正确的,我只使用空字符串只提供电子邮件和密码.这可能与网站使用https的事实有关吗?我的代码似乎在没有https的开发框中正常工作,但这可能是一个红色的鲱鱼.
唯一似乎涵盖我的要求的堆栈溢出问题没有得到接受的答案,文档也没有我能看到的帮助.
我现在脑袋里有这么大的凹痕,撞在一堵砖墙上,我正想着拿一顶方帽子.
如果有人能给我解决方案,我会永远欠你的债!我甚至可能通过电子邮件发送给你一个KitKat
Jen*_*ens 17
这个简单的例子滥用了HttpWatch上的一个页面,因为它对于一个有效的公共示例更有趣.
有问题的资源https://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?randomgarbage使用HTTPS上的基本身份验证,并且可以在没有身份验证失败的情况下加载(使用Android 2.3.7测试):
WebView v = ...; // Your webview goes here.
try {
HashMap<String, String> map = new HashMap<String, String>();
// This test service takes the username "httpwatch" and a random
// password. Repeating a password can lead to failure, so we create
// a decently random one using UUID.
String usernameRandomPassword = "httpwatch:" + UUID.randomUUID().toString();
String authorization = "Basic " + Base64.encodeToString(usernameRandomPassword.getBytes("UTF-8"), Base64.NO_WRAP);
map.put("Authorization", authorization);
v.loadUrl("https://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?" + System.currentTimeMillis(), map);
} catch (UnsupportedEncodingException e) {}
Run Code Online (Sandbox Code Playgroud)
这适用于ICS和Gingerbread.不能访问比这更早的东西,但是loadUrl(String, Map<String,String>)在API级别8中引入了,所以我不明白它为什么不能用于此.
Nappy的澄清:
要支持对后续请求的身份验证,请提供WebViewClient并执行以下操作:
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url, <your map containing the Authorization header>);
return true;
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16349 次 |
| 最近记录: |