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;
    }
    
  • 相关阅读:
    hdu_5961_传递(bitset)
    hdu_5963_朋友(找规律)
    hdu_5968_异或密码(预处理+二分)
    hdu_5969_最大的位或(贪心)
    hdu_5965_扫雷(递推)
    hdu_5950_Recursive sequence(矩阵快速幂)
    hdu_5286_wyh2000 and sequence(分块)
    [BZOJ1951][SDOI2005]古代猪文(数论好题)
    [BZOJ2659][WC2012]算不出的算式(几何)
    [BZOJ 2656][ZJOI2012]数列(递归+高精度)
  • 原文地址:https://www.cnblogs.com/anjou/p/3114084.html
Copyright © 2011-2022 走看看