具有 https 连接和基本身份验证的 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);
正如你所看到的,我确实有一个覆盖的 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 个请求)才是现在的重点!
我读到,我可以通过使用抢先提供凭据
webview.setHttpAuthUsernamePassword(Config.SERVER_BASE_URL, "Application", email, pwd);
,但这只会导致 Http Basic:访问被拒绝错误 服务器基本 url 常量是网站的域名,即 https://example.com (不含页面)实际值网址为 https://example.com/some_pages。 无论我使用完整的 url 还是域名都没有区别。我已经检查了领域,结果是正确的,并且我只使用了仅提供电子邮件和密码的空字符串。 这是否与该网站使用 https 有关?我的代码似乎在没有 https 的开发盒上运行良好,但这可能是一个转移注意力的事情。!
似乎涵盖我的要求的唯一堆栈溢出问题尚未得到接受的答案,并且我看不到文档没有任何帮助。
现在我的头因为撞到砖墙上而留下了很大的凹痕,所以我正在考虑买一顶方形帽子。
如果有人能给我解决这个问题的方法,我将永远感激你! 我什至可能会通过电子邮件给您发送奇巧巧克力
I have researched and researched and researched this until I've gone grey and bald.
How on earth do I get a webview to work for a site that needs http basic authentication over an https connection on api level 8+
I have the following code
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);
As you can see I did have a web view client (Now commented out) that overrides the onReceivedHttpAuthRequest method which looks like this
@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);
}
}
This was used without the webview.setHttpAuthUsernamePassword and works fine except that it means 2 requests are issued to the website - The first gets a 401 and then the client kicks in with the authorisation stuff This is fine for a small amount of website traffic but halving the amount of traffic (currently averaging 49 requests p/m) is the name of the game right now!
I read that I can pre-emptively supply the credentials by using
webview.setHttpAuthUsernamePassword(Config.SERVER_BASE_URL, "Application", email, pwd);
However this just results in Http Basic: Access denied errors
The server base url constant is the domain name for the site i.e. https://example.com (without the page) the actual url is https://example.com/some_pages.
It makes no difference whether I use the full url or the domain. I have checked the realm and I have that correct and I have used just empty strings providing only email and password.
Could this be something to do with the fact that the site is using https? My code seems to work fine on my dev box without https but that may be a red herring.!
The only stack overflow questions that seem to cover my requirements have not got accepted answers and the docs are no help that I can see.
I now have such a large dent in my head from banging it against a brick wall that I am thinking of getting a square hat.
Please if anyone can give me the solution to this I will be forever in your debt!
I might even e-Mail you an KitKat
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个简单的示例滥用了 HttpWatch 上的页面,因为使用公开的示例会更有趣。
有问题的资源,
https://www.httpwatch.com /httpgallery/authentication/authenticatedimage/default.aspx?randomgarbage
使用 HTTPS 上的基本身份验证,并且可以在没有身份验证失败的情况下加载,如下所示(使用 Android 2.3.7 进行测试):这适用于 ICS 和 Gingerbread。无法访问比这更旧的任何内容,但
loadUrl(String, Map)
是在 API 级别 8 中引入的,所以我不明白为什么它不适合那个到。Nappy 说明:
为了支持后续请求的身份验证,您需要提供
WebViewClient
并执行以下操作:This simple example abuses a page on HttpWatch since it's more fun with a working public example.
The resource in question,
https://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?randomgarbage
uses basic auth over HTTPS and can be loaded without authentication failure like this (tested using Android 2.3.7):This works on ICS and Gingerbread. Don't have access to anything older than that, but
loadUrl(String, Map<String,String>)
was introduced in API level 8, so I don't see why it shouldn't work for that to.Clarification for Nappy:
To support authentication for subsequent requests you supply a
WebViewClient
and do the following:它将适用于 https URL。如果我们得到 Untrusted_cer 那么我们将忽略它
我不知道第二个问题
It will work for https URL.In this if we are getting Untrusted_cer then we will ignore it
I have no idea about second problem
要处理站点上的授权,只需覆盖 webViewClient 中的以下方法并在其处理程序中添加用户名和密码。
for handling authorization on your site, just override the following method in your webViewClient and add username and password in its handler.