【问题标题】:How to get a Windows Phone 7's useragent string?如何获取 Windows Phone 7 的用户代理字符串?
【发布时间】:2014-04-22 20:26:58
【问题描述】:

我需要获取手机的用户代理字符串,但我在 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

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

【问题讨论】:

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


    【解决方案1】:

    使用相关电话转到http://whatsmyuseragent.com

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

    【讨论】:

      【解决方案2】:

      我创建了这个帮助程序,它将创建一个临时 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)";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多