WebView.setHttpAuthUsernamePassword() not working?
Asked Answered
G

3

0

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?

Grim answered 28/6, 2010 at 17:46 Comment(2)
BTW, ended up using the full browser to access the site.Grim
Check out question 2585055. Try setting up a WebViewClient...Watermelon
M
1

You can use this:

webview.setWebViewClient(new WebViewClient() {
    @Override
    public void onReceivedHttpAuthRequest(WebView view,
        HttpAuthHandler handler, String host, String realm) {
            handler.proceed("username", "password");
    }
});
Mangosteen answered 3/6, 2016 at 12:39 Comment(0)
A
0

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.

Arrival answered 28/6, 2010 at 18:25 Comment(2)
no, it's not using NTLM auth. still trying to figure this one out. thanks though.Grim
Android ICS version and up supports NTLM auth.Agoraphobia
M
0

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

Maidamaidan answered 9/12, 2013 at 19:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.