我使用的是自定义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;
谢谢你的帮助!
你可以试试下面的代码。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;
}
}
}