在WebView上设置的请求标头未通过

时间:2018-12-13 16:03:28

标签: c# android xamarin xamarin.forms

我已经实现了一个自定义Web视图渲染器,并带有一个自定义WebViewClient,如下所示。但是,新的标题并没有到达页面。我错过了什么?

“ otherheader”使它通过(但仅在第一个请求时),但“ mysource”却没有。

WebViewClient

public class MyWebViewClient : WebViewClient
{
    public override WebResourceResponse ShouldInterceptRequest(WebView view, IWebResourceRequest urlResource)
    {
        if (!urlResource.RequestHeaders.ContainsKey("mysource"))
        {
            urlResource.RequestHeaders.Add("mysource", "app");
        }

        return base.ShouldInterceptRequest(view, urlResource);
    }
}

CustomRenderer

[assembly: ExportRenderer(typeof(HybridWebView), typeof(HybridWebViewRenderer))]
namespace CustomRenderer.Droid
{
    public class HybridWebViewRenderer : ViewRenderer<HybridWebView, Android.Webkit.WebView>
    {
        Context _context;

        public HybridWebViewRenderer(Context context) : base(context)
        {
            _context = context;
        }

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

            if (Control == null)
            {
                var webView = new Android.Webkit.WebView(_context);
                webView.Settings.JavaScriptEnabled = true;
                webView.SetWebViewClient(new MyWebViewClient());
                SetNativeControl(webView);
            }
            if (e.OldElement != null)
            {
                var hybridWebView = e.OldElement as HybridWebView;
                hybridWebView.Cleanup();
            }
            if (e.NewElement != null)
            {
                IDictionary<string, string> headers = new Dictionary<string, string>();
                headers.Add("otherheader", "yes");
                Control.LoadUrl(e.NewElement.Uri, headers);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您应该返回一个新的# -*- coding: utf-8 -*- import re text = 'ValidCharactersForPassword123._-+.AndRemoveNonAscii-áéíóú' output = re.sub(r'[^A-Za-z0-9_+.-]+','',text) print(output) ValidCharactersForPassword123._-+.AndRemoveNonAscii- 而不是WebResourceResponse

例如:

return base.ShouldInterceptRequest(view, urlResource);