How to Handle Basic Authentication in Webview

BasicAuthentication in android for webview not working

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");
}
}

This is most voted solution there I am not sure where to set the URL to open.Please suggest.

android webview with https connection and basic auth. How to get this working?

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):

    WebView v = ...; // Your webview goes here. 
try {
HashMap<String, String> map = new HashMap<String, String>();
// This test service takes the username "httpwatch" and a random
// password. Repeating a password can lead to failure, so we create
// a decently random one using UUID.
String usernameRandomPassword = "httpwatch:" + UUID.randomUUID().toString();
String authorization = "Basic " + Base64.encodeToString(usernameRandomPassword.getBytes("UTF-8"), Base64.NO_WRAP);
map.put("Authorization", authorization);
v.loadUrl("https://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?" + System.currentTimeMillis(), map);
} catch (UnsupportedEncodingException e) {}

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:

    webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url, <your map containing the Authorization header>);
return true;
}
});

Flutter WebView with Basic Authentication

Add your additional headers to the WebviewScaffold constructor.

    url: widget.url,
withJavascript: true,
headers: {'Authorization': 'Basic ' + base64Encode(utf8.encode('$widget.username:$widget.password'))},

Pass username and password into the widget, in the same way that you are passing the url.

Supply some basic auth credentials to a WebView?

You can call:

 setHttpAuthUsernamePassword (String host, String realm, String username, String password)

read more here

WebView Authentication

I am not sure what you mean by saying "it isn't basic authentication".

When you set up your WebView, do you give it a WebViewClient that implements onReceivedError()? I believe that would be the vector for knowing that a 401 had occurred and requesting authentication by the user.

Alternately, use onReceivedHttpAuthRequest() and pass the username/pwd to the handler via handler.proceed(username,pwd);

AFAIK a WebView doesn't deal with the popup that you'll see in Chrome by itself. Implementing something like that is left to the developer. You create whatever authentication dialogue you require to request credentials from the user. If this is what you want, you might refer to this thread.



Related Topics



Leave a reply



Submit