我已经研究和研究过这个问题,直到我变得灰白秃顶。 我怎么得到一个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);
正如您所看到的,我确实有一个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);
}
}
这是在没有webview.setHttpAuthUsernamePassword的情况下使用并且工作正常,除了它意味着向网站发出2个请求 - 第一个获得401然后客户端使用授权内容启动这对于少量网站来说很好流量但流量减半(目前平均49个请求p / m)是游戏的名称!
我读到我可以使用
先发制人地提供凭据webview.setHttpAuthUsernamePassword(Config.SERVER_BASE_URL, "Application", email, pwd);
然而,这只会导致Http Basic:Access拒绝错误 服务器基本网址常量是网站的域名,即https://example.com(没有网页)实际网址为https://example.com/some_pages。 无论是使用完整网址还是域名都没有区别。我检查了领域,我有正确的,我只使用空字符串只提供电子邮件和密码。 这可能与网站使用https的事实有关吗?我的代码似乎在没有https的开发框中正常工作,但这可能是一个红色的鲱鱼。!
似乎涵盖我的要求的唯一堆栈溢出问题没有得到接受的答案,文档没有我能看到的帮助。
我现在脑袋里有这么大的凹痕,撞在一堵砖墙上,我正想着拿一顶方帽子。
如果有人能给我解决方案,我会永远欠你的债! 我甚至可能通过电子邮件发送给你一个KitKat
答案 0 :(得分:16)
这个简单的示例滥用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) {}
这适用于ICS和Gingerbread。无法访问早于此的任何内容,但API级别8中引入了loadUrl(String, Map<String,String>)
,因此我不明白为什么它不适用于此。
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;
}
});
答案 1 :(得分:1)
它适用于https URL。如果我们收到Untrusted_cer,那么我们将忽略它
webview.setWebViewClient(new WebViewClient(){
@Override
public void onReceivedHttpAuthRequest(WebView view,
HttpAuthHandler handler, String host, String realm) {
super.onReceivedHttpAuthRequest(view, handler, host, realm);
}
@Override
public void onReceivedSslError(WebView view,
SslErrorHandler handler, SslError error) {
super.onReceivedSslError(view, handler, error);
if(error.getPrimaryError()==SslError.SSL_UNTRUSTED){
handler.proceed();
}else{
handler.proceed();
}
}
});
我不知道第二个问题
答案 2 :(得分:0)
用于处理您网站上的授权,只需在您的webViewClient中覆盖以下方法,并在其处理程序中添加用户名和密码。
ui-sref