WebView.setHttpAuthUsernamePassword() 不起作用?

发布于 2024-09-07 12:34:59 字数 628 浏览 5 评论 0原文

我正在开发 Android 应用程序的一部分,需要使用 WebView 打开受密码保护的网站。我使用 SharedPreferences 提供用户首次登录应用程序时的用户名和密码。我已经测试了它返回的凭据,所以我知道这些是正确的。当我在模拟器中运行它时,该网站说我未经授权(即使我是)。这是代码:

  setContentView(R.layout.browser);
  WebView browser = (WebView) findViewById(R.id.browser);
  browser.getSettings().setJavaScriptEnabled(true);
  SharedPreferences credentials = getSharedPreferences("credentials", 0);
  browser.setHttpAuthUsernamePassword("example.com", "", credentials.getString("username", ""), credentials.getString("password", ""));
  browser.loadUrl("http://example.com");

那么有人知道为什么这不能验证我的身份吗?我放置“”的领域字符串实际上应该是什么吗?

I am developing part of an Android application that needs to use a WebView to open a password protected site. I am using SharedPreferences to provide the username and password from when the user logs in the app for the first time. I've tested the credentials it's returning, so I know that those are correct. When I run this in the emulator, the site says that I'm unauthorized (even though I am). Here's the code:

  setContentView(R.layout.browser);
  WebView browser = (WebView) findViewById(R.id.browser);
  browser.getSettings().setJavaScriptEnabled(true);
  SharedPreferences credentials = getSharedPreferences("credentials", 0);
  browser.setHttpAuthUsernamePassword("example.com", "", credentials.getString("username", ""), credentials.getString("password", ""));
  browser.loadUrl("http://example.com");

So does anyone know why this wouldn't be authenticating me? Should the realm string that I put "" for actually be something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伪心 2024-09-14 12:35:00

你可以使用这个:

webview.setWebViewClient(new WebViewClient() {
    @Override
    public void onReceivedHttpAuthRequest(WebView view,
        HttpAuthHandler handler, String host, String realm) {
            handler.proceed("username", "password");
    }
});

You can use this:

webview.setWebViewClient(new WebViewClient() {
    @Override
    public void onReceivedHttpAuthRequest(WebView view,
        HttpAuthHandler handler, String host, String realm) {
            handler.proceed("username", "password");
    }
});
眼泪都笑了 2024-09-14 12:35:00

如果站点使用 NTLM 身份验证,它将无法工作。 Android 本身不支持 NTLM Auth。 Fennec(firefox mobile)是我见过的唯一支持它的 Android 浏览器,但它仍处于 alpha 阶段。

if the site is using NTLM auth it won't work. Android does not natively support NTLM Auth. Fennec (firefox mobile) is the only android browser i've seen that supports it, but its still in alpha.

夏了南城 2024-09-14 12:35:00

Android 声称现在支持 NTLM。我不知道这是 WebView 的一部分还是只是浏览器的一部分。

https://code.google.com/p/android/issues/detail ?id=4962

Android claims to support NTLM now. I don't know if that is part of the WebView or just the browser.

https://code.google.com/p/android/issues/detail?id=4962

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文