使用c#解析用户代理

时间:2010-05-25 13:08:25

标签: c# regex

我有一大堆用户代理字符串。 对于分析面板,我需要解析它们并拆分为:

  1. 浏览器名称,版本。 2.平台名称,版本
  2. 如果有现成的解决方案或代码来执行此操作? 就像那里:http://useragentstring.com/

4 个答案:

答案 0 :(得分:3)

我知道这很糟糕但我找不到更好的东西,

public static class UserAgentParser
{
    /// <summary>
    /// Extracts human readible Operating system name.
    /// </summary>
    /// <param name="userAgent">User Agent string from Request.</param>
    /// <returns>Human readible Operating system name.</returns>
    public static string GetOperatingSystem(string userAgent)
    {
        var clientOsName = string.Empty;
        if (userAgent.Contains("Windows 98"))
            clientOsName = "Windows 98";
        else if (userAgent.Contains("Windows NT 5.0"))
            clientOsName = "Windows 2000";
        else if (userAgent.Contains("Windows NT 5.1"))
            clientOsName = "Windows XP";
        else if (userAgent.Contains("Windows NT 6.0"))
            clientOsName = "Windows Vista";
        else if (userAgent.Contains("Windows NT 6.1"))
            clientOsName = "Windows 7";
        else if (userAgent.Contains("Windows NT 6.2"))
            clientOsName = "Windows 8";
        else if (userAgent.Contains("Windows"))
        {
            clientOsName = GetOsVersion(userAgent, "Windows");
        }
        else if (userAgent.Contains("Android"))
        {
            clientOsName = GetOsVersion(userAgent, "Android");
        }
        else if (userAgent.Contains("Linux"))
        {
            clientOsName = GetOsVersion(userAgent, "Linux");
        }
        else if (userAgent.Contains("iPhone"))
        {
            clientOsName = GetOsVersion(userAgent, "iPhone");
        }
        else if (userAgent.Contains("iPad"))
        {
            clientOsName = GetOsVersion(userAgent, "iPad");
        }
        else if (userAgent.Contains("Macintosh"))
        {
            clientOsName = GetOsVersion(userAgent, "Macintosh");
        }
        else
        {
            clientOsName = "Unknown OS";
        }

        return clientOsName;
    }

    private static string GetOsVersion(string userAgent, string osName)
    {
        if (userAgent.Split(new[] {osName}, StringSplitOptions.None)[1].Split(new[]{';',')'}).Length != 0)
            {
                return string.Format("{0}{1}", osName,userAgent.Split(new[] { osName }, StringSplitOptions.None)[1].Split(new[] { ';', ')' })[0]);
            }

        return osName;
    }
}

[TestFixture]
public class UserAgentParserTest
{

    public IEnumerable<TestCaseData> UserAgentStringTestData()
    {
        yield return new TestCaseData("Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3", "iPhone");
        yield return new TestCaseData("Mozilla/5.0 (iPad; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3", "iPad");
        yield return new TestCaseData("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Ubuntu/11.04 Chromium/17.0.963.56 Chrome/17.0.963.56 Safari/535.11", "Linux x86_64");
        yield return new TestCaseData("Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11", "Linux i686");
        yield return new TestCaseData("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11", "Windows 7");
        yield return new TestCaseData("Mozilla/5.0 (Windows NT 6.0; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11", "Windows Vista");
        yield return
            new TestCaseData(
                "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4", "Windows 8");
        yield return
            new TestCaseData(
                "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3", "Windows XP");
        yield return
            new TestCaseData(
                "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US) AppleWebKit/532.0 (KHTML, like Gecko) Chrome/3.0.198 Safari/532.0",
                "Windows 2000");
        yield return
            new TestCaseData(
                "Mozilla/5.0 (compatible; MSIE 7.0; Windows 98; SpamBlockerUtility 6.3.91; SpamBlockerUtility 6.2.91; .NET CLR 4.1.89;GB)", "Windows 98");
        yield return
            new TestCaseData(
                "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/537.13 (KHTML, like Gecko) Chrome/24.0.1290.1 Safari/537.13",
                "Macintosh");
        yield return
            new TestCaseData(
                "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",
                "Unknown OS");
        yield return
            new TestCaseData(
                "Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30",
                "Android 4.0.3");
        yield return
            new TestCaseData("Mozilla/1.22 (compatible; MSIE 10.0; Windows 3.1)", "Windows 3.1");
    }

    [Test]
    [TestCaseSource("UserAgentStringTestData")]
    public void UserAgentParsesOs(string userAgent, string expectedOs)
    {
        Assert.AreEqual(expectedOs, UserAgentParser.GetOperatingSystem(userAgent));
    }
}

答案 1 :(得分:2)

查看用户代理字符串语法的RFC 1945RFC 2068

答案 2 :(得分:1)

我建议向遇到此问题的任何人安装DannyBoyNg's UserAgentParser,因为可以通过NuGet轻松下载和安装它。

对于您想要获得的信息,这是您将要做的:

using UserAgentParser;
...
var ua = UserAgent.Parse(Request.UserAgent.ToString());
string b = ua.Browser.ToString() + ", " + ua.BrowserVersion.ToString();
string p = ua.Platform.ToString();

对不起,这里的 exact 版本不提供任何支持,因此,如果您想区分Windows 10 1607版和1903版,那就很不幸了。

您需要做的另一件事是在Web.config

中的 assembly 标记中添加一行额外内容
<add assembly="netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51"/>

我希望您不必这样做,但目前看不到任何解决方法。

答案 3 :(得分:1)

从.nuget安装OrbintSoft.Yauaa.NetStandard软件包

1)创建此类来解析您的UserAgent:

public static class YauaaSingleton
{
    private static UserAgentAnalyzer.UserAgentAnalyzerBuilder Builder { get; }

    private static readonly Lazy<UserAgentAnalyzer> analyzer = new Lazy<UserAgentAnalyzer>(() => Builder.Build());

    public static UserAgentAnalyzer Analyzer
    {
        get
        {
            return analyzer.Value;                
        }
    }

    static YauaaSingleton()
    {
        Builder = UserAgentAnalyzer.NewBuilder();
        Builder.DropTests();
        Builder.DelayInitialization();
        Builder.WithCache(100);
        Builder.HideMatcherLoadStats();
        Builder.WithAllFields();            
    }
}

2)像这样处理您的用户代理:

public void ProcessUserAgents(string[] userAgents)
{
   foreach (var userAgent in userAgents)
   {
        var parserdUA = YauaaSingleton.Analyzer.Parse(userAgent);
        var browserNameVersion = parserdUA.GetValue(UserAgent.AGENT_NAME_VERSION);
        //There is no a single field for platform name/version:
        var osNameVersion = parserdUA.GetValue(UserAgent.OPERATING_SYSTEM_NAME_VERSION);
        var deviceName = parserdUA.GetValue(UserAgent.DEVICE_NAME);
        var deviceVersion = parserdUA.GetValue(UserAgent.DEVICE_VERSION);

        //If you want all fields:
        var fieldNames = ua.GetAvailableFieldNames();
        foreach (var name in fieldNames)
        {
            var field = ua.Get(name);
            Console.WriteLine($"name: {name}, value: {field.GetValue()}, confidence: {field.GetConfidence()}"});            
        }

   }
}

3)解析示例:

UserAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36 OPR/67.0.3575.137'

ParsingOutput