basquang™ on clouds

April 26, 2014

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

Filed under: Microsoft Technology,Tips,Windows Phone — basquang @ 10:07 AM
Tags: , ,

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("https://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("https://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/

Advertisements

15 Comments »

  1. 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

    Comment by brijesh tekrawala — July 3, 2014 @ 5:08 PM | Reply

  2. 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

    Comment by redefinecareer — July 3, 2014 @ 6:47 PM | Reply

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

    Comment by basquang — July 7, 2014 @ 9:31 AM | Reply

  4. 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…

    Comment by Craig — October 1, 2014 @ 1:22 AM | Reply

    • have you found another way?

      Comment by joao — December 19, 2014 @ 12:52 AM | Reply

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

      Comment by Mostafiz Rahman — August 17, 2015 @ 6:52 PM | Reply

  5. Hi,
    I am worried about is importing Dll allowed in Marketplace App?

    Comment by Scott — October 27, 2014 @ 1:29 PM | Reply

  6. 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.

    Comment by joao — December 19, 2014 @ 12:51 AM | Reply

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

    Comment by whitespace — April 22, 2015 @ 9:33 PM | Reply

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

    Comment by sujeet — July 11, 2015 @ 7:10 PM | Reply

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

    Comment by Andy Burgess — November 27, 2015 @ 5:41 AM | Reply

  10. 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.

    Comment by Martin Suchan — October 7, 2016 @ 4:51 PM | Reply

    • Exactly. Any other possibilities?

      Comment by Seb — February 12, 2017 @ 3:51 AM | Reply

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

    Comment by Friday — May 12, 2017 @ 3:55 PM | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: