开发者

How to get a Windows Phone 7's useragent string?

开发者 https://www.devze.com 2023-01-29 18:25 出处:网络
I need to get a phone\'s user agent string, but I haven\'t found anything in the API that allows this. I\'ve come across the following two blog posts describing the format of the user agent string:

I need to get a phone's user agent string, but I haven't found anything in the API that allows this. I've come across the following two blog posts describing the format of the user agent string:

http://blogs.msdn.com/b/iemobile/archive/2010/03/25/ladies-and-gentlemen-please-welcome-the-ie-mobile-user-agent-string.aspx

http://madskristensen.net/post/Windows-Phone-7-user-agents.aspx

But I haven't found a method that can return the user a开发者_StackOverflowgent. Has anyone been able to do this successfully?


Go to http://whatsmyuseragent.com using the phone in question.

From my Samsung Focus: Mozilla/4.0 (compatible: MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0; SAMSUNG; SGH-i917)


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)";
0

精彩评论

暂无评论...
验证码 换一张
取 消