How to display a protected Blog into a WebView (setHttpAuthUsernamePassword)
From setHttpAuthUsernamePassword, you need to pass in the host of the site: so if you are accessing (for example) http://www.google.com with username and password from your code and realm Protected
, you would do
webview.setHttpAuthUsernamePassword("www.google.com", "Protected", getString(R.string.username), getString(R.string.password));
You can get the realm by looking at the published realm in your browser: for example, in Firefox if you open http://tools.dynamicdrive.com/password/example/ you will see that the following text in the caption: A username and password are being requested by http://tools.dynamicdrive.com. The site says: "Restricted Area. In this case the realm is Restricted Area.
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;
}
});
Related Topics
How Does One Implement Drag and Drop for Android Marker
How to Copy Selected Files from Android with Adb Pull
How to Use Mediacodec Without Mediaextractor for H264
How to Get the Key from the Value in Firebase
How to Check the Internet Connection Periodically in Whole Application
No Notification Sound When Sending Notification from Firebase in Android
How to Use Textview.Getlayout()? It Returns Null
How to Get the Dreaded Write_Secure_Settings Permission for My Android App
How to Add a Hint in Spinner in Xml
Network_Provider Not Providing Updated Locations
Android: Custom Separator (Or Even Item) in Listview Depening on Content of Item
Upload an Image Using Google Volley
How to Merge Two Mp3 Files into One (Combine/Join)
What Happens If a Android Service Is Started Multiple Times
Gesture Detection and Scrollview Issue
How to Check If Music Is Playing by Using a Broadcast Receiver