Sending custom HTTP headers with a GET-request and the MS-WebView in Windows 8.1 Store apps

Alex wrote this on 9 March 2014 in Microsoft Technical

Recently I had the need to send some custom HTTP headers when a WebView component used in our app performed a GET request. In this post I will show you how this is done.

When searching for this functionality you will soon discover the NavigateWithHttpRequestMessage method of the component. But the documentation says that this method only works for POST methods, not for GET. So, some experimenting was in order. Using the XAML below:


 

 

 

 

 

 

 

 

 

 

 

 

And the following C# code:


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

You can see on lines 25-28 how the call to the NavigateWithHttpRequestMessage method is configured. I was surprised to see that this call just works, even when the documentation is stating that it shouldn’t. A quick look with fiddler:

 

confirms that indeed a GET request is performed and that my custom header is present.

The final test for me was to verify that this would also work using HTML/JavaScript:


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

On lines 22 –26 you can find the JavaScript performing the call to the NavigateWithHttpRequestMessage method. Another look with fiddler:

 

confirms that this also works.

Conclusion

You can use the NavigateWithHttpRequestMessage method to send custom headers with both POST and GET requests. Note that you can’t set a cookie this way. By the way, one of the best posts about the new features in the WebView component in Windows 8.1 is this one.

Leave a comment

avatar

Your email address will not be published.

Tags: , , , , ,