I am using a custom webview client CustomWebviewClient : FormsWebViewClient
In the overridden function ShouldOverrideUrlLoading(Android.Webkit.WebView webView, IWebResourceRequest request)
I can see the request object, but the RequestHeaders
are null.
In my HybridWebViewRenderer : WebViewRenderer
I'm adding additional headers for my app version like so:
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
Am I missing something here on the Android side?
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:
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.
[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;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.