BasicAuthentication in android for webview not working

若如初见. 提交于 2019-12-01 03:59:09
Ali
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.

This should be perfect code snippet.

    webView.getSettings().setJavaScriptEnabled(true);
    webView.setFocusable(true);
    webView.loadUrl("http://3864.cloud-matic.net/");
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onReceivedHttpAuthRequest(WebView view,
                                              HttpAuthHandler handler, String host, String realm) {
            handler.proceed("me@test.com", "mypassword");
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            prDialog.setMessage("Please wait ...");
            prDialog.show();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (prDialog != null) {
                prDialog.dismiss();
            }
        }
    });

Based on answer in another topic I made it in a little different way.

This solution is working also in case, that you have auto login form displayed, when authentication failed (in such case, solution with overriding 'onReceivedHttpAuthRequest' is not working, because you are getting a normal html page without error).

String up = "yourusername"+":"+"yoursuperstrongpassword";
String authEncoded = new String(Base64.encodeBase64(up.getBytes()));
String authHeader = "Basic "+authEncoded;
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", authHeader);
myWebView.loadUrl("https://192.168.137.1:8443/tmedserver/doctor/doctorConsultations/consultationCall.mvc?consultationId=23", headers);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!