Cloud Training (Online)




How to Set Custom User Agent for a WebView in Windows 10 ?

When working with the WebView in Universal Windows Platform app , there are times when you want to pass a custom user agent along with the request.

The WebView in Windows 10 includes a method called NavigateWithHttpRequestMessage which lets you pass the object of type HttpRequestMessage. We could use the Header property of the HttpRequestMessage and set the user agent of the request as shown below.

How to Set Custom User Agent for a WebView in Windows 10 ?

var httpRequestMessage = new Windows.Web.Http.HttpRequestMessage(Windows.Web.Http.HttpMethod.Get, new Uri("http://DeveloperPublish.com"));
httpRequestMessage.Headers.Add("User-Agent", "DeveloperPublishUserAgent");
webView1.NavigateWithHttpRequestMessage(httpRequestMessage);
Senthil Kumar B
Senthil Kumar is a former Microsoft MVP (Most Valuable Professional). He is a Co-Author of the book "Windows 10 Development Recipes using JavaScript and CSS" for Apress Publication. He is a technical presenter, blogger, mentor and a Geek.  Senthil is a regular speaker is various local user groups. He has presented at conferences like Great Indian Developer Summit (GIDS) & Microsoft DevCamps. You can reach out to him via his Twitter handle @isenthil.