如何获得Windows Phone 7的useragent字符串?

时间:2010-12-07 00:41:55

标签: silverlight windows-phone-7 user-agent

我需要获取手机的用户代理字符串,但我没有在API中找到允许这样做的任何内容。我遇到了以下两篇博客文章,描述了用户代理字符串的格式:

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

但是我没有找到可以返回用户代理的方法。有没有人能够成功地做到这一点?

3 个答案:

答案 0 :(得分:4)

使用相关手机转到http://whatsmyuseragent.com

来自我的三星焦点:Mozilla/4.0 (compatible: MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0; SAMSUNG; SGH-i917)

答案 1 :(得分:3)

使用WebBrowser和一些脚本在此处发布了检索用户代理的解决方案。

Getting the Browser's User Agent.

答案 2 :(得分:0)

我做了这个帮助器,它将创建一个临时的WebBrowser,加载一个脚本并返回一个等待的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;
    }
}

用法:

var userAgent = await UserAgentHelper.GetUserAgent();

它至少适用于WP7.1和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)";