maui
maui copied to clipboard
Custom http headers set in WebViewHandler are not sent
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
Xamarin result
I also included the Xamarin sample and the result is correct.
Steps to Reproduce
- Open the sample solution
- 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
This is really important for our application as well please look into this
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());
}
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.
@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:
- Override
ShouldInterceptRequest
- If request already has the header, return
base.ShouldInterceptRequest
- if not, then
- call
LoadUrl
on the native view with original URL and additional headers - return an empty
WebResourceResponse
for the current request.
- call
Not very elegant but that was the only way it worked on Xamarin.
@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.
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
any update on this i am also facing the same issue
News about the issue ?
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.