使用的WebView setHttpAuthUsernamePassword?WebView、setHttpAuthUsernamePassword

2023-09-12 06:28:13 作者:北辰

我想要做的基本身份验证,以查看受保护的URL。我要访问受保护的URL看起来像这样:

I'm trying to do basic authentication to view a protected url. I want to access the protected url which looks like this:

http://api.test.com/userinfo/vid?=1234

所以,我做一个的WebView以下内容:

So I do the following with a WebView:

mWebView.setHttpAuthUsernamePassword("api.test.com", "", "me@test.com", "mypassword");
mWebView.loadUrl("http://api.test.com/userinfo/user?uid=53461");

而认证似乎并没有工作,我只是得到一个输出错误页面。我使用的web视图的方法正确吗?

but the authentication doesn't seem to work, I'm just getting an output error page. Am I using the WebView method correctly here?

更新: 试图与卷曲:

curl -u me@test.com:mypassword http://api.test.com/userinfo/user?uid=53461

和它拉页罚款。我尝试了许多参数的每个组合,API的业主不知道我说的境界虽然(和我也不) - 可能我给什么信息他们帮助这个沿

and it pulls the page fine. I tried every combination of the host parameter, the owners of the api don't know what I mean by 'realm' though (and neither do I) - what info could I give them to help this along?

感谢

推荐答案

另一种选择是使用WebViewClient;

Another option is to use a WebViewClient;

webview.setWebViewClient(new MyWebViewClient ());

private class MyWebViewClient extends WebViewClient {
@Override
public void onReceivedHttpAuthRequest(WebView view,
        HttpAuthHandler handler, String host, String realm) {

    handler.proceed("me@test.com", "mypassword");

}
}