Java android 中用于 webview 的 BasicAuthentication 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21185400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:10:36  来源:igfitidea点击:

BasicAuthentication in android for webview not working

javaandroidwebviewbasic-authentication

提问by Ali

Hi I want to open this url http://3864.cloud-matic.net/from my android webview and I have tried many ways but the app even not opens mainActivity. What I have tried is below.

嗨,我想从我的 android webview打开这个 url http://3864.cloud-matic.net/,我尝试了很多方法,但该应用程序甚至没有打开 mainActivity。我尝试过的内容如下。

 public void onCreate(Bundle state) {
    super.onCreate(state);
    setContentView(R.layout.main);
    WebView webView = (WebView) findViewById(R.id.webView1);
    webView.setHttpAuthUsernamePassword("cloud-matic.net/",     "realm", username, password);
    webView.loadUrl("http://3864.cloud-matic.net/");
}

Please give me idea where I am wrong. Ali

请告诉我我错在哪里。阿里

采纳答案by Ali

webview.setWebViewClient(new MyWebViewClient ());

private class MyWebViewClient extends WebViewClient {
    @Override
    public void onReceivedHttpAuthRequest(WebView view,
            HttpAuthHandler handler, String host, String realm) {
        handler.proceed("[email protected]", "mypassword");
    }
}

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

这是投票最多的解决方案,我不确定在哪里设置要打开的 URL。请建议。

回答by Axay Coolmoco

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("[email protected]", "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();
            }
        }
    });

回答by Krystian

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

此解决方案也适用于在身份验证失败时显示自动登录表单的情况(在这种情况下,覆盖“onReceivedHttpAuthRequest”的解决方案不起作用,因为您获得了一个没有错误的正常 html 页面)。

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