1
votes

I have to send User-agent to the server from my app.

"Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0; HTC; HD7 T9292)" The above is a sample user-agent. I have no idea. What are all the values we can get programmatically in order to form the user-agent.

3

3 Answers

2
votes

For the record, the device-name isn't a necessary part of the User-Agent.

The global User-Agent string for Mango looks like this:

Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0)
0
votes

That looks like the user agent of IE7 mobile on a HD7. I'm assuming that you are not writing IEMobile and so your app will have a different user agent name/identifier. If you're making web requests from your app then your app is a different user agent to the built in browser.

The SDK does not expose a way of getting all the individual elements that make up the user agent in your sample.

If you really wanted to get the user agent of the browser, from within your app, the best you could probably do would be to include a WebBrowser control and then extract it via Javascript with navigator.userAgent but I don't know if it's supported. If it is supported you could pass it back to your code behind using windows.external.notify.

0
votes

I made this helper that will create a temporary WebBrowser, load a script and return an awaitable userAgent:

internal static class UserAgentHelper
{
    private const string Html = @"<!DOCTYPE html><html><body onload=""window.external.notify(navigator.userAgent);""></body></html>";
    public static Task<string> GetUserAgent()
    {
        var tcs = new TaskCompletionSource<string>();
        var browser = new WebBrowser { IsScriptEnabled = true };
        browser.ScriptNotify += (sender, args) => tcs.SetResult(args.Value);
        browser.NavigateToString(Html);
        return tcs.Task;
    }
}

Usage:

var userAgent = await UserAgentHelper.GetUserAgent();

It works at least for WP7.1 and WP8.0:

WP7: "Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; Microsoft; XDeviceEmulator)";
WP8: "Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; Microsoft; Virtual)";