zoukankan      html  css  js  c++  java
  • ASP.NET 根据 UserAgent 获取浏览器的类型和版本

    //using System.Text.RegularExpressions;
    public string GetBrowserName(string userAgent, out string browserName, out string ver)
    {
        string fullBrowserName = string.Empty;
        browserName = string.Empty;
        ver = string.Empty;
        // IE
        string regexStr = @"msie (?<ver>[\d.]+)";
        Regex r = new Regex(regexStr, RegexOptions.IgnoreCase);
        Match m = r.Match(userAgent);
        if (m.Success)
        {
            browserName = "IE";
            ver = m.Groups["ver"].Value;
            fullBrowserName = string.Format("{0} {1}", browserName, ver);
            return fullBrowserName;
        }
        // Firefox
        regexStr = @"firefox\/([\d.]+)";
        r = new Regex(regexStr, RegexOptions.IgnoreCase);
        m = r.Match(userAgent);
        if (m.Success)
        {
            browserName = "IE";
            ver = m.Groups["ver"].Value;
            fullBrowserName = string.Format("{0} {1}", browserName, ver);
            return fullBrowserName;
        }
        // Chrome
        regexStr = @"chrome\/([\d.]+)";
        r = new Regex(regexStr, RegexOptions.IgnoreCase);
        m = r.Match(userAgent);
        if (m.Success)
        {
            browserName = "IE";
            ver = m.Groups["ver"].Value;
            fullBrowserName = string.Format("{0} {1}", browserName, ver);
            return fullBrowserName;
        }
        // Opera
        regexStr = @"opera.([\d.]+)";
        r = new Regex(regexStr, RegexOptions.IgnoreCase);
        m = r.Match(userAgent);
        if (m.Success)
        {
            browserName = "IE";
            ver = m.Groups["ver"].Value;
            fullBrowserName = string.Format("{0} {1}", browserName, ver);
            return fullBrowserName;
        }
        // Safari
        regexStr = @"version\/([\d.]+).*safari";
        r = new Regex(regexStr, RegexOptions.IgnoreCase);
        m = r.Match(userAgent);
        if (m.Success)
        {
            browserName = "IE";
            ver = m.Groups["ver"].Value;
            fullBrowserName = string.Format("{0} {1}", browserName, ver);
            return fullBrowserName;
        }
        return fullBrowserName;
    }
    
  • 相关阅读:
    [BJOI2012]最多的方案(记忆化搜索)
    our happy ending(状压dp)
    [NOI2005]聪聪与可可(期望dp)
    CF983A Finite or not?(数学)
    [POI2012]STU-Well(二分答案+神仙操作)
    作诗2(玄学)
    IncDec Sequence(差分)
    [Vani有约会]雨天的尾巴(树上差分+线段树合并)
    合法括号序列(dp+组合数学)
    [SHOI2014]概率充电器(概率+换根dp)
  • 原文地址:https://www.cnblogs.com/anjou/p/3114084.html
Copyright © 2011-2022 走看看