Currently, I am setting the User-Agent of a request message as follows:
var rm = new HttpRequestMessage(HttpMethod.Post, new Uri("http://example.com"));
rm.Headers.Add("User-Agent", "UserAgentString");
webView.NavigateWithHttpRequestMessage(rm);
Everything works as expected.
But, when I am navigating to an another page, by clicking a link on the site for example, the User-Agent resets to the default of the WebView
.
Is there any way of setting the User-Agent permanently or changing it on every request made?
Thanks, George
I found this trick by Matt Dot. This will permanently change the user-agent string. Any
WebView
request, whether manual or through a link-click inside the HTML, will have your value sent as the User-Agent header.Here's the source in case the link dies.
You can change this value anywhere in your app, but if you want it permanently set, use the
App.xaml.cs
constructor:The only way I've been able to get this to work fairly reliably is by using the NavigationStarting Event to cancel the page load and get the current url, then use a custom event to trigger the new page load with the correct UA.
Not pretty, but it seems to work.
NavigationStarting
occurs before the webview navigates to new content. You can cancel that operation get theargs.Uri
and navigate withHttpRequestMessage
. There is alsoFrameNavigationStarting
.