Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom http headers set in WebViewHandler are not sent #12206

Open
programatix opened this issue Dec 19, 2022 · 9 comments
Open

Custom http headers set in WebViewHandler are not sent #12206

programatix opened this issue Dec 19, 2022 · 9 comments
Labels
area-controls-webview WebView platform/android 🤖 platform/iOS 🍎 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Milestone

Comments

@programatix
Copy link

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 programatix added the t/bug Something isn't working label Dec 19, 2022
@jfversluis jfversluis added legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor area-controls-webview WebView labels Dec 19, 2022
@SarthakGz
Copy link

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

@programatix
Copy link
Author

programatix commented Dec 20, 2022

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());
        }

@rachelkang rachelkang added this to the Backlog milestone Dec 20, 2022
@ghost
Copy link

ghost commented Dec 20, 2022

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.

@rbakhshi
Copy link

rbakhshi commented Dec 22, 2022

@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.

@programatix
Copy link
Author

@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.

@Zhanglirong-Winnie Zhanglirong-Winnie added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Jul 10, 2023
@Zhanglirong-Winnie
Copy link

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

@manicoder
Copy link

any update on this i am also facing the same issue

@mnel75
Copy link

mnel75 commented Oct 5, 2023

News about the issue ?

@programatix
Copy link
Author

programatix commented Nov 20, 2023

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.

@Eilon Eilon removed the legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor label May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-webview WebView platform/android 🤖 platform/iOS 🍎 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

10 participants