maui icon indicating copy to clipboard operation
maui copied to clipboard

Custom http headers set in WebViewHandler are not sent

Open programatix opened this issue 2 years ago • 5 comments

Description

Custom headers set in WebViewHandler are not sent. On my test, both Android and iOS have this issue, so I believe somehow, MAUI subsystem overwritten the headers.

I have prepared a sample solution at https://github.com/programatix/MauiWebViewBug. The test solution provided opens the URL https://manytools.org/http-html-text/http-request-headers which will echo the request headers.

The sample sets the following headers

  • "Authorization": "bearer hello"
  • "Accept-Language": "US"
  • "MyHeader": "there"
MAUI result

image image

Xamarin result

I also included the Xamarin sample and the result is correct. image

Steps to Reproduce

  1. Open the sample solution
  2. Run

Link to public reproduction project repository

https://github.com/programatix/MauiWebViewBug

Version with bug

7.0 (current)

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android, I was not able test on other platforms

Affected platform versions

Android 13, iOS 16.1.1

Did you find any workaround?

No response

Relevant log output

No response

programatix avatar Dec 19 '22 09:12 programatix

This is really important for our application as well please look into this

SarthakGz avatar Dec 19 '22 10:12 SarthakGz

I found a workaround on this by overriding CreatePlatformView() in the handlers. The original MauiWebView (Android) and MauiWKWebView (iOS) somehow overwritten the headers loaded by our LoadUrl/LoadRequest call.

I also found something weird with MAUI WebView control. If I ignore the Source property and introduced a user defined property in MyWebView, and later use that in the handlers as the URL source to perform LoadUrl() (Android) or LoadRequest() (iOS), the WebView will be blank. This is also true after I use the workaround mentioned above. I guess the page is loaded but not rendered by the WebView control?

Below is the workaround to get the headers transmitted.

Android

using static Android.Views.ViewGroup;
...
        protected override global::Android.Webkit.WebView CreatePlatformView()
        {
            var view = new global::Android.Webkit.WebView(Context!)
            {
                LayoutParameters = new LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent)
            };

            view.Settings.JavaScriptEnabled = true;
            view.Settings.DomStorageEnabled = true;
            view.Settings.SetSupportMultipleWindows(true);

            return view;
        }

iOS

        protected override WKWebView CreatePlatformView()
        {
            return new WKWebView(CoreGraphics.CGRect.Empty, new WKWebViewConfiguration());
        }

programatix avatar Dec 20 '22 02:12 programatix

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost avatar Dec 20 '22 19:12 ghost

@programatix are you sure this ever worked in Xamarin? We had a similar need and ended up intercepting the request and injecting the headers there.

Basically:

  1. Override ShouldInterceptRequest
  2. If request already has the header, return base.ShouldInterceptRequest
  3. if not, then
    1. call LoadUrl on the native view with original URL and additional headers
    2. return an empty WebResourceResponse for the current request.

Not very elegant but that was the only way it worked on Xamarin.

rbakhshi avatar Dec 22 '22 05:12 rbakhshi

@rbakhshi Yes, it works in Xamarin and I have been using it since last year. The sample solution I provided has Xamarin version in it.

programatix avatar Dec 22 '22 05:12 programatix

Verified this issue with Visual Studio Enterprise 17.7.0 Preview 2.0. Can repro on iOS and android platform with sample project. MauiWebViewBug.zip 12206

Zhanglirong-Winnie avatar Jul 10 '23 07:07 Zhanglirong-Winnie

any update on this i am also facing the same issue

manicoder avatar Jul 16 '23 18:07 manicoder

News about the issue ?

mnel75 avatar Oct 05 '23 14:10 mnel75

Issue is still there for .NET 7.0. For .NET 8.0, I can't test it since my macbook doesn't support Ventura.

MauiWKWebView implements IWebViewDelegate which caused LoadUrl to be called. It overwrites any platformView.LoadRequest() called in the handler's ConnectHandler function. Its parameter is only url, thus whatever headers set in the request will be lost.

programatix avatar Nov 20 '23 11:11 programatix