[WP8.1]: Changing Windows Phone 8.1 WebView default User-Agent in all outbound HTTP requests

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!

Reference: http://www.lukepaynesoftware.com/articles/programming-tutorials/changing-the-user-agent-in-a-web-browser-control/

This Post Has 0 Comments

  1. brijesh tekrawala

    I am searching for same thing and i got this solution as above, is there anyway we can do this for windows phone 7?


  2. redefinecareer

    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?


  3. basquang

    Hi, This code is not work for Windows Phone 7/8

  4. Craig

    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…

    1. joao

      have you found another way?

    2. Mostafiz Rahman

      But I need to change the user agent to render MathJax. Is there any other way to do so?

  5. Scott

    I am worried about is importing Dll allowed in Marketplace App?

  6. joao

    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.

  7. whitespace

    Hi guys,
    I am getting folowing error:
    System.NotSupportedException : DllImport cannot be used on user-defined methods

  8. sujeet

    UrlMkSetSessionOption us not suported for Windows Phone or Windows Desktop Store.

  9. Andy Burgess

    Thanks for posting this – it just saved my project! (Well, the deadline anyway!)

  10. Martin Suchan

    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.

    1. Seb

      Exactly. Any other possibilities?

  11. Friday

    I was working on a project that required this functionality, getting the same issue as “whitespace” above.Has anyone found another way?

Leave a Reply