简体   繁体   English

使用 ShouldOverrideUrlLoading 从 Xamarin.Forms Android webview 检索标头时遇到问题,总是 (null)

[英]Having trouble retrieving headers from Xamarin.Forms Android webview using ShouldOverrideUrlLoading, always (null)

I am using a custom webview client CustomWebviewClient : FormsWebViewClient我正在使用自定义 webview 客户端CustomWebviewClient : FormsWebViewClient

In the overridden function ShouldOverrideUrlLoading(Android.Webkit.WebView webView, IWebResourceRequest request) I can see the request object, but the RequestHeaders are null.在重写的函数ShouldOverrideUrlLoading(Android.Webkit.WebView webView, IWebResourceRequest request)我可以看到请求对象,但RequestHeaders为空。

In my HybridWebViewRenderer : WebViewRenderer I'm adding additional headers for my app version like so:在我的HybridWebViewRenderer : WebViewRenderer我为我的应用程序版本添加了额外的标题,如下所示:

Dictionary<string, string> headers = new Dictionary<string, string>
                {
                    {Constants.AppHeader, Constants.AppVersion }
                };

Control.LoadUrl(uri, headers);

I can see these headers on my site when I dump them, but the IWebResourceRequest.RequestHeaders are always null当我转储它们时,我可以在我的网站上看到这些标头,但IWebResourceRequest.RequestHeaders始终为null

Am I missing something here on the Android side?我在 Android 方面遗漏了什么吗?

I've noticed a couple times on the iOS side that I need to force cast certain objects in order to access their properties/methods like so:我在 iOS 方面注意到几次,我需要强制强制转换某些对象才能访问它们的属性/方法,如下所示:

var thisresponse = (NSHttpUrlResponse)navigationResponse.Response;

var allOfThem = thisresponse.AllHeaderFields;

Thanks for the help!谢谢您的帮助!

You could try the code below.你可以试试下面的代码。 CustomHeaderValue is the BindableProperty of my CustomHeaderWebView. CustomHeaderValue是我的 CustomHeaderWebView 的 BindableProperty。

  [assembly: ExportRenderer(typeof(CustomHeaderWebView), typeof(CustomHeaderWebViewRenderer))]
namespace App14.Droid
{
public class CustomHeaderWebViewRenderer : ViewRenderer<CustomHeaderWebView, Android.Webkit.WebView>
{
    Context _localContext;

    public CustomHeaderWebViewRenderer(Context context) : base(context)
    {
        _localContext = context;
    }

    protected override void OnElementChanged(ElementChangedEventArgs<CustomHeaderWebView> e)
    {
        base.OnElementChanged(e);

        Android.Webkit.WebView webView = Control as Android.Webkit.WebView;

        if (Control == null)
        {
            webView = new Android.Webkit.WebView(_localContext);
            SetNativeControl(webView);
        }

        if (e.NewElement != null)
        {
            Dictionary<string, string> headers = new Dictionary<string, string>
            {
                ["Authorization"] = Element.CustomHeaderValue // Change this string for a different header key
            };

            webView.Settings.JavaScriptEnabled = true;

            webView.Settings.BuiltInZoomControls = true;
            webView.Settings.SetSupportZoom(true);

            webView.ScrollBarStyle = ScrollbarStyles.OutsideOverlay;
            webView.ScrollbarFadingEnabled = false;

            webView.SetWebViewClient(new CustomWebViewClient(headers));
            UrlWebViewSource source = Element.Source as UrlWebViewSource;
            webView.LoadUrl(source.Url, headers);
        }
    }
}

public class CustomWebViewClient : Android.Webkit.WebViewClient
{
    public Dictionary<string, string> headers { get; set; }

    public CustomWebViewClient(Dictionary<string, string> requestHeaders)
    {
        headers = requestHeaders;
    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM