I recently came into a UWP project requiring all HTTP requests to use a specific User-Agent string.
That’s quite easy to do if you only use Windows.Web.Http.HttpClient as there’s a managed property for that purpose: HttpClient.DefaultRequestHeaders.UserAgent.
Similarly, the same applies for System.Net.Http.HttpClient and the HttpClient.DefaultHeader.UserAgent property.
The real problem is if you need to use the WebView control to present web content, as there’s no managed property to change the user-agent!
It is possible to create a HttpRequestMessage instance, set a custom user-agent string in it, and then call the WebView.NavigateWithHttpRequestMessage method, but that will only work for that specific navigation request.
Any requests invoked from inside the webview (like a form post) will use the system default user-agent string.
So what’s the solution?
The solution comes in the form of a Win32 API called UrlMkSetSessionOption.
Amongst other things, this API allows you to set the default user-agent string for the current internet session!
Here’s an example of how to use it; first, add the following class to your app:
public class UserAgentHelper
{
[DllImport("urlmon.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
private static extern int UrlMkSetSessionOption(int dwOption, string pBuffer, int dwBufferLength, int dwReserved);
private const int URLMON_OPTION_USERAGENT = 0x10000001;
public static void SetDefaultUserAgent(string userAgent)
{
UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, userAgent, userAgent.Length, 0);
}
}
Now load your App.xaml.cs file and ensure you call the UserAgentHelper.SetDefaultUserAgent()
method inside the App
class constructor:
public App()
{
// remaining code
UserAgentHelper.SetDefaultUserAgent("MyApp/1.0");
}
From that point on, any webview inside your app will use the specified user-agent string (“MyApp/1.0” in our example above)!
One final note: only a few Win32 API’s are actually allowed in apps published to the Windows Store (you can check the complete list here)!
The Windows Store recently allowed for apps to use the UrlMkSetSessionOption
API, but if you look at the list above you will notice that it’s not yet listed; that should change once Microsoft updates the list on that page.
Kudos to Hermit Dave for pointing me in the right direction for a solution for this problem!