如何在WebView中处理基本身份验证 [英] How to handle Basic Authentication in WebView
问题描述
我已经创建了一个加载WebView的应用程序.为了登录, 该网站需要基本身份验证.当我尝试访问该网站时 通过默认的浏览器,我会看到一个弹出框,提示我输入用户名 和密码.
I've created an application which loads a WebView. In order to login, the website requires basic authentication. When i try to access the website via the default browser, I get a pop up box prompting me to enter my user name and password.
如果我尝试通过我的应用程序访问该网站,则会收到错误401,并且没有弹出窗口. 我想知道是否有人可以帮助我?
If I try to access the website via my application, I get error 401 and no pop up. I was wondering if someone could help me out?
推荐答案
我认为,比Patrick所描述的解决方案更优雅的解决方案是使用WebViewClient的onReceivedHttpAuthRequest
方法,如下所述:
I reckon a more elegant solution than the one Patrick describes would be to use the onReceivedHttpAuthRequest
method of WebViewClient as described here: http://www.mail-archive.com/android-developers@googlegroups.com/msg30468.html
@Override
public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
handler.proceed("username", "password");
}
这篇关于如何在WebView中处理基本身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!