2

I'm working on a UWP app and have a page with a WebView. In the WebView I need to set the user-agent to a custom value.

I have tried the following:

var requestMessage = new HttpRequestMessage(HttpMethod.Get, baseUri);
requestMessage.Headers.Add("User-Agent", "MyCustomValue");
webview.NavigateWithHttpRequestMessage(requestMessage);

However the WebView doesn't use my custom user-agent but instead use the original default value of the user-agent. This is confirmed by this thread at MSDN.

Any good input to alternative solutions or workarounds is appreciated.

Community
  • 1
  • 1
thomasmartinsen
  • 1,983
  • 14
  • 21
  • There's a workaround. It's not the most elegant solution, but it works: http://stackoverflow.com/questions/39490430/change-default-user-agent-in-webview-uwp – Laith Apr 29 '17 at 00:03

2 Answers2

1

It seems only to be supported when doing POST, not GET.

Perhaps this blog post can get you closer to a solution: https://basquang.wordpress.com/2014/04/26/wp8-1-changing-windows-phone-8-1-webview-default-user-agent-in-all-outbound-http-requests/

  • Good input - however calling NavigateWithHttpRequestMessage on the WebView will send the RequestMessage as POST (according to their documentation). I tried to change the HttpMethod to POST instead of GET - didn't change anything :/ – thomasmartinsen Jan 05 '16 at 10:27
1

Try it:

var rm = new Windows.Web.Http.HttpRequestMessage(HttpMethod.Get, new Uri("https://www.whatismybrowser.com/detect/what-http-headers-is-my-browser-sending")); 
rm.Headers.Add("User-Agent", "test");
rm.Headers.Add("NSASESSIONID", "CA79AB9B-21CD-43BE-A48A-49B5F1289D22");
WebView.NavigateWithHttpRequestMessage(rm);

It's working for me.

Dmytro Bondarenko
  • 865
  • 1
  • 7
  • 12