【问题标题】:BasicAuthentication in android for webview not workingandroid中用于webview的BasicAuthentication不起作用
【发布时间】:2014-01-17 11:56:50
【问题描述】:

嗨,我想从我的 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/");
}

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

【问题讨论】:

  • "app even not opens mainActivity" 您是否遇到某种错误或崩溃或只是一个空白屏幕?
  • 你能提供一个logcat吗?
  • 首先我需要问什么是“领域”,它在 setHttpAuthUsernamePassword 函数中使用。如果我在共享 logcat 之前缺少一些值?
  • 现在 logcat 中没有任何内容,应用程序也没有打开。但是在控制台中得到这个:成功!在设备 4df1e6190ef38f11 ActivityManager 上启动活动 com.commonsware.android.appwidget.lorem.LoremActivity:正在启动:Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.commonsware.android。 appwidget.lorem/.LoremActivity }
  • 我会说这是一个重复的问题。在这里查看您的问题的答案(投票最多的一个):stackoverflow.com/questions/2585055/…

标签: java android webview basic-authentication


【解决方案1】:
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");
    }
}

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

【讨论】:

  • 有没有试过在设置网页视图客户端后添加webView.loadUrl("http://3864.cloud-matic.net/");
  • 嗨,是的,我在我的 onCreate 方法中使用了它。 webView.loadUrl("3864.cloud-matic.net/"); webView.setWebViewClient(new MyWebViewClient());
  • 调用loadUrl时需要提供http://的URL,即webView.loadUrl("http://3864.cloud-matic.net");
  • Alex 我和你提到的一样,并提供了正确的凭据,但仍然需要 401 授权。当我尝试从网络浏览器登录时,相同的密码和用户名正在工作。
  • 阿里,如果它在浏览器中运行,并且通过这种方法你仍然可以通过你的应用程序获得401,可能是你没有使用Basic认证,而是Digest。您能否在浏览器控制台中确认您从客户端发送的内容是Authorization: Basic 还是Authorization: Digest
【解决方案2】:

不确定这是否会对某人有所帮助,但我使用用户名和密码实现了 onReceivedHttpAuthRequest 功能(如上面的人),并且在除 Android Oreo 之外的所有设备上都能正常工作。然后,我意识到奥利奥手机收到了不是来自主机的http请求,它给了我401错误。

在我检查的onReceivedHttpError函数中,如果http错误来自主框架:

         `override fun onReceivedHttpError(
                view: WebView?,
                request: WebResourceRequest?,
                errorResponse: WebResourceResponse?
            ) {
                super.onReceivedHttpError(view, request, errorResponse)
                if(request.isForMainFrame){ //do smt. with the error}
               }`

然后就开始正常工作了。

【讨论】:

    【解决方案3】:

    这应该是完美的代码sn-p。

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

    【讨论】:

      【解决方案4】:

      基于另一个主题的答案,我以不同的方式进行了回答。

      此解决方案也适用于,当您显示自动登录表单时,身份验证失败(在这种情况下,覆盖“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);
      

      【讨论】:

        猜你喜欢
        • 2013-04-08
        • 2023-03-23
        • 2012-12-29
        • 2011-06-27
        • 2018-11-09
        • 2018-11-14
        • 2015-03-22
        • 1970-01-01
        • 2011-11-24
        相关资源
        最近更新 更多