0

I'm trying to change the User Agent of a Webview in a Windows 8 app. At the moment, I'm currently doing this:

    [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;

Then if I needed to change the UA to the iPhone for example:

            string ua = "Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X)" + 
"AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25";
            UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, ua, ua.Length, 0);

This works perfectly. But unfortunately, if I'm going to submit this app to the Windows Store, I won't be able to use this method since UrlMkSetSessionOption is not officially supported in Windows 8 apps.

Upon searching for alternatives I came across this. However, although that works, the page renders horribly, with barely any images or other elements being shown.

So I hoped that another way to do this would be by using the WebView.InvokeScript method and injecting some JavaScript to change the UA. The problem being, to be quite honest, is that I'm a complete beginner when it comes to JavaScript.

I did a bit more searching and apparently something like this (found here) would work:

navigator.__defineGetter__('userAgent', function(){
return 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25' // customized user agent
});

navigator.userAgent; // 'foo'

But like I said, I'm a complete beginner with Javascript. So when exactly should I inject the code? Do I need to listen for an event in JavaScript that runs this code when it fires?

Community
  • 1
  • 1
  • Tha JS solution can't work IMHO since UserAgent ist already sent with the first HTTP GET going to the server - long before ANY JS is executed! – Yahia Jan 05 '13 at 16:09
  • Are you sure? This is where I found the JS code, and according to those users it seemed to work. http://stackoverflow.com/a/1307088/1949034 –  Jan 05 '13 at 19:00
  • Yes I am sure... the solution works for "JS unit testing" - i.e. inside the page when running JS - it can't (by definition) work before any HTML/JS is even received... the browser sends a user agent in HTTP request which happens BEFORE any JS can even run! – Yahia Jan 05 '13 at 20:46
  • Ok, I understand. I guess I'll have to wait until there's an official WinRT supported solution available. Thanks for the help. –  Jan 05 '13 at 23:09

1 Answers1

0

it's easy.

 string ua = "Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X)" + "AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25";
            Uri targetUri = new Uri(url);
            HttpRequestMessage hrm=new HttpRequestMessage(HttpMethod.Get,targetUri);
            hrm.Headers.Add("User-Agent",ua);
            webView1.NavigateWithHttpRequestMessage(hrm);
John TiXor
  • 73
  • 8
  • Would this also work for all subsequest requests? I mean all the calls for .js, .css, .json and so on – Marandil Aug 07 '15 at 10:50