【问题标题】:Having trouble retrieving headers from Xamarin.Forms Android webview using ShouldOverrideUrlLoading, always (null)使用 ShouldOverrideUrlLoading 从 Xamarin.Forms Android webview 检索标头时遇到问题,始终(空)
【发布时间】:2021-12-01 11:18:09
【问题描述】:

我正在使用自定义 webview 客户端CustomWebviewClient : FormsWebViewClient

在重写的函数ShouldOverrideUrlLoading(Android.Webkit.WebView webView, IWebResourceRequest request) 中我可以看到请求对象,但RequestHeaders 为空。

在我的HybridWebViewRenderer : WebViewRenderer 中,我正在为我的应用版本添加额外的标题,如下所示:

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

Control.LoadUrl(uri, headers);

当我转储它们时,我可以在我的网站上看到这些标题,但 IWebResourceRequest.RequestHeaders 始终是 null

我在 Android 方面是否遗漏了什么?

我注意到在 iOS 端有几次我需要强制转换某些对象才能访问它们的属性/方法,如下所示:

var thisresponse = (NSHttpUrlResponse)navigationResponse.Response;

var allOfThem = thisresponse.AllHeaderFields;

感谢您的帮助!

【问题讨论】:

  • 关于这个话题有很多现有的问题 - google.com/…
  • @Jason 从表面上看,这是不可能的,对吗?这总是null?
  • 我没有做任何深入的研究,但我扫描的几个链接暗示这只是一个奇怪的 Android 行为
  • @Jason 谢谢,在我的测试中似乎是这样。

标签: android xamarin.forms webview xamarin.android android-webview


【解决方案1】:

你可以试试下面的代码。 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;
    }
}
}

【讨论】:

    猜你喜欢
    • 2019-05-16
    • 1970-01-01
    • 2018-06-10
    • 2015-04-05
    • 1970-01-01
    • 2019-12-24
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    相关资源
    最近更新 更多