Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set custom WebView header in UWP

This could seem to be duplicates of other similar questions but they are old thread and not specific to Windows UWP apps.

I'm unable to set custom header in WebView so the loaded URLs in WebView could work for me.

I have seen many forums giving solution like using HttpClient/WebRequest with header but that doesn't work as in my case, the web address usage Javascript for redirection and before redirection it needs few custom header to load correctly.

Also WebView.NavigateWithHttpRequestMessage is not so suitable as it will postback and I need the headers for each request including javascript redirected URLs in web View.

I'm able to set custom headers in Xamarin.Droid project using Renderers but I couldn't find any solution for UWP Windows.UI.Xaml.Controls.WebView.

like image 935
Vishnu Avatar asked Aug 26 '16 11:08

Vishnu


2 Answers

On Universal Windows 10 Platform, the WebView.NavigateWithHttpRequestMessage method is the right way.

a. I need the headers for each request including javascript redirected URLs in web View.

b. This didn't resolve my issue as after setting the headers the OnWebViewNavigationStarting method is called multiple times and App crashes automatically with System.StackOverflowException error

This is due to the infinite navigation will happen if we do navigation in the NavigationStarting event. We should cancel navigation in a handler for this event by setting the WebViewNavigationStartingEventArgs.Cancel property to true.

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

Code sample:

    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 info in Fiddler, the request record in the second red rectangle included the custom header:

enter image description here

I shared my UWP sample in here, you can easily integrate into your Xamarin UWP app.

like image 148
Franklin Chen - MSFT Avatar answered Nov 14 '22 18:11

Franklin Chen - MSFT


With the Xamarin Tag, it seems like you are using this with Xamarin.Forms and hence the below answer is with respect to Xamarin.Forms. However, the code holds true for WebView in UWP as well.

You can try creating a Custom Renderer for WebView and then try making use of the same WebView.NavigateWithHttpRequestMessage.

Before navigating you can try setting the Headers like this:

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

The Uri above can be set based on your requirement.

In case the request is happening multiple times, you can always set the delegate for NavigationStarting event and handle it in the method.

Control.NavigationStarting += OnWebViewNavigationStarting
like image 1
Anubhav Ranjan Avatar answered Nov 14 '22 16:11

Anubhav Ranjan