Recently, I’m developing Windows Phone 8 app which hosting our cloud services. We need to add some custom HTTP Headers in to requests. Unfortunately, Windows Phone 8 WebBrowser control doesn’t have properties to set custom User-Agent by default. The only way you can do is the method:
webBrowser1.Navigate(new Uri("http://basquang.wordpress.com/2014/04/26/wp8-1-changing-windows-phone-8-1-webview-default-user-agent-in-all-outbound-http-requests/", UriKind.Absolute), null, "User-Agent: your custom user-agent");
But with this method, you are able to pass your custom user-agent in first request only. With outbound HTTP request, meaning all web link in your site, the webBrowser1 will using device’s default User-Agent.
You can check your current user-agent here: http://whatsmyuseragent.com/
By the release of Windows Phone 8.1, now you can set your custom User-Agent for entire process by using Win32 API: UrlMkSetSessionOption
Using following code to make your user-agent by default for all outbound HTTP request in WebView control on Windows Phone 8.1
[DllImport("urlmon.dll", CharSet = CharSet.Ansi)] private static extern int UrlMkSetSessionOption(int dwOption, string pBuffer, int dwBufferLength, int dwReserved); const int URLMON_OPTION_USERAGENT = 0x10000001; public void ChangeUserAgent(string Agent) { UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, Agent, Agent.Length, 0); } void MainPage_Loaded(object sender, RoutedEventArgs e) { ChangeUserAgent("My Custom User-Agent"); wb.Navigate(new Uri("http://basquang.wordpress.com/2014/04/26/wp8-1-changing-windows-phone-8-1-webview-default-user-agent-in-all-outbound-http-requests/", UriKind.Absolute)); }
Hope this help!
Hi,
I am searching for same thing and i got this solution as above, is there anyway we can do this for windows phone 7?
Thanks
Brijesh
Hi ,
i am using this code in WP8 project and i am getting following error.
Attempt to access the method failed: Binja.MainPage.UrlMkSetSessionOption(System.Int32, System.String, System.Int32, System.Int32)
Any idea?
Broksj
Hi, This code is not work for Windows Phone 7/8
While this does work, apps produced with this approach that are submitted to the store will fail certification as the API’s aren’t part of the permitted set…
have you found another way?
Did you find?
But I need to change the user agent to render MathJax. Is there any other way to do so?
Hi,
I am worried about is importing Dll allowed in Marketplace App?
certification test fails with this aproach, and i can’t find another solution to the “post” info beiing lost …. or keeping the user-agent active.
Hi guys,
I am getting folowing error:
System.NotSupportedException : DllImport cannot be used on user-defined methods
UrlMkSetSessionOption us not suported for Windows Phone or Windows Desktop Store.
Thanks for posting this – it just saved my project! (Well, the deadline anyway!)
I’ve just tested this API in Windows 10 UWP app. It works, BUT Windows App Certification Kit fails with this message, so it’s a no-go for us:
Error Found: The supported APIs test detected the following errors:
API UrlMkSetSessionOption in urlmon.dll is not supported for this application type. UATest.UWP.dll calls this API.
Exactly. Any other possibilities?
I was working on a project that required this functionality, getting the same issue as “whitespace” above.Has anyone found another way?