1

How can I set the header in the webrequest to a website In Maui (.Net 7) WebView Control Android

.SetHeader("?", value) type thing?

Thanks in advance

I've tried setting it via the navigation event but can't seem to find a function to set the header

DWK
  • 11
  • 2

2 Answers2

1

There is no a such api can add a header into the webview's request in the maui. You need to use the android native code.

Refer to this case about how to update request header in WebView in the native android, you can use the android native WebViewCilent and override the shouldInterceptRequest or the shouldOverrideUrlLoading to add a header.

But there is a bug about Android WebViewClient's ShouldInterceptRequest is never called in MAUI WebView. The custom handler will not work until the bug has been fixed. You can follow up this issue on the github.

So you can try to use the custom renderer to do that, you can try the code in this answer about using the webvew custom renderer in the maui. I have tested it, but the webview will be blank in my device. It seems also a bug. But you can have a try.

Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14
1

This was not an easy one, I've been working on this for that last 3 days but finally got it working.

What you'll need:

  • Custom WebViewClient
  • Custom MauiBlazorWebViewHandler

Under platforms => Android, add your custom WebViewClient, this will be a deravitave of WebViewClient but to maintain Blazor functionality will also take the current/existing WebViewClient as a parameter which will be used where necessary. In the ShouldOverrideUrlLoading we will load the original url but we will pass additional headers and finish of the method with calling the original client's method.

     public class CustomAndroidWebViewClient: WebViewClient
    {
        private readonly WebViewClient _client;

        public CustomAndroidWebViewClient(WebViewClient client)
        {
            _client = client;
            _deviceId = deviceId;
        }

        public override WebResourceResponse ShouldInterceptRequest(WebView view, IWebResourceRequest request)
        {
            return _client.ShouldInterceptRequest(view, request);
        }

        public override bool ShouldOverrideUrlLoading(WebView? view, IWebResourceRequest? request)
        {
            view.LoadUrl(request.Url.ToString(), new Dictionary<string, string>() { { "MyHeader", "HeaderValue" } });
            return _client.ShouldOverrideUrlLoading(view, request);
        }

        public override void OnPageFinished(WebView? view, string? url)
            => _client.OnPageFinished(view, url);

        protected override void Dispose(bool disposing)
        {
            if (!disposing)
                return;

            _client.Dispose();
        }

    }

Next will be the custom MauiBlazorWebViewHandler, this will replace the default handler of the BlazorWebView and will replace the base WebViewClient with our custom WebViewClient and will inherit from the default BlazorWebViewHandler. This can be placed in the root space of your application or in a specific folder.

 public sealed partial class MauiBlazorWebViewHandler : BlazorWebViewHandler
{

    public MauiBlazorWebViewHandler()
    {          
    }

    public override void SetMauiContext(IMauiContext mauiContext)
    {           
        base.SetMauiContext(mauiContext);
    }

    protected override void ConnectHandler(WebView platformView)
    {
        base.ConnectHandler(platformView);

        platformView.Settings.JavaScriptEnabled = true;
        platformView.SetWebViewClient(
            new CustomAndroidWebViewClient(platformView.WebViewClient));
    }
}

To make it all work, in MauiProgram.cs add:

#if ANDROID
    builder.ConfigureMauiHandlers(handlers => {
        handlers.AddHandler<IBlazorWebView, MauiBlazorWebViewHandler>();
    });
#endif

NOTE: I'm using a #if ANDROID block because i've only implemented this handler for Android and do not need any of this functionality on other platforms.

  • No matter what I try and do, the references just won't play nice with any of this. Could you include your using statements at the top? – Murphybro2 Jun 28 '23 at 10:12