Setting a custom WebView header in UWP

This may seem like a duplicate of other similar questions, but they are old threads and not specific to Windows UWP applications.

I cannot set a custom header in WebView so that the loaded URLs in WebView can work for me.

I have seen many forums giving a solution such as using an HttpClient / WebRequest with a header, but this does not work, as in my case, using the Javascript web address for redirection, and before redirecting it needs a little custom header to load correctly.

Also, WebView.NavigateWithHttpRequestMessage is not suitable as it will be returned, and I need headers for each request, including the redirected javascript URLs in the webview .

I can set custom headers in a Xamarin.Droid project using Renderers, but I could not find a solution for UWP Windows.UI.Xaml.Controls.WebView .

+6
source share
2 answers

On the universal platform of Windows 10, the WebView.NavigateWithHttpRequestMessage method is the correct way.

a. I need headers for every request, including javascript redirected URLs in the web view.

b. This did not solve my problem, because after setting the headers, the OnWebViewNavigationStarting method OnWebViewNavigationStarting called several times, and the application starts automatically with a System.StackOverflowException error

This is due to endless navigation if we are navigating in a NavigationStarting event. We must cancel the navigation in the handler of this event by setting the WebViewNavigationStartingEventArgs.Cancel property to true .

And we need to add / remove a handler for the NavigationStarting event.

Code example:

  private void NavigateWithHeader(Uri uri) { var requestMsg = new Windows.Web.Http.HttpRequestMessage(HttpMethod.Get, uri); requestMsg.Headers.Add("User-Name", "Franklin Chen"); wb.NavigateWithHttpRequestMessage(requestMsg); wb.NavigationStarting += Wb_NavigationStarting; } private void Button_Click(object sender, RoutedEventArgs e) { NavigateWithHeader(new Uri("http://openszone.com/RedirectPage.html")); } private void Wb_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args) { wb.NavigationStarting -= Wb_NavigationStarting; args.Cancel = true;//cancel navigation in a handler for this event by setting the WebViewNavigationStartingEventArgs.Cancel property to true NavigateWithHeader(args.Uri); } 

The screenshot is the log information in Fiddler , the request record in the second red box included the user header:

enter image description here

I shared my UWP example in here , you can easily integrate into your Xamarin UWP application.

+6
source

With the Xamarin Tag, it seems like you are using this with Xamarin.Forms, and therefore the answer below relates to Xamarin.Forms. However, the code is also valid for WebView in UWP.

You can try creating custom rendering for the WebView, and then try using the same WebView.NavigateWithHttpRequestMessage.

Before navigating, you can try setting the title as follows:

 var requestMsg = new Windows.Web.Http.HttpRequestMessage(HttpMethod.Get, new Uri("https://www.whatismybrowser.com/detect/what-http-headers-is-my-browser-sending")); requestMsg.Headers.Add("User-Name", "AnubhavRanjan"); Control.NavigateWithHttpRequestMessage(requestMsg); 

Uri above can be set based on your requirement.

If the request is executed several times, you can always set a delegate for the NavigationStarting event and process it in the method.

 Control.NavigationStarting += OnWebViewNavigationStarting 
+1
source

All Articles