将外部 URL 加载到 WebView

Loading external URL in to WebView

Laoding external URL 在 WebView 中有带有用户名和密码的对话框。它给我错误 需要授权

URL "http://developer.xamarin.com:8081/api/todoitems"

我加载了很多 urls 但没有收到这样的错误。但是如上所述加载 URL 和对话框用于登录它给我错误。

请帮助我如何在 WebView 中显示这样的 url。

您需要提供该页面所需的凭据:

myWebview.SetHttpAuthUsernamePassword(host, realm, username, password);

Xamarin Reference

您可以创建 Native WebView Renderer。 将 WebViewClient 添加到此 webview 渲染器:

webView.SetWebViewClient(new MyWebViewClient ());

如下创建 MyWebViewClient 并覆盖 OnReceivedHttpAuthRequest :

class MyWebViewClient : WebViewClient
{
    public override void OnReceivedHttpAuthRequest(Android.Webkit.WebView view, HttpAuthHandler handler, string host, string realm)
    {
        Dialog dialog = new Dialog(view.Context);

        dialog.SetContentView(/*Layout here*/);
        dialog.Show();

        //On submit Button Click from Layout set username and password:
        handler.Proceed(userName, password);
    }
}

对话框是 android 项目中的布局,包含用户名、密码、提交和取消字段。您可以创建自己的或从 some pages.

获得帮助