• C# 服务端获取客户端 系统/浏览器/IP


            /// <summary>
            /// 获取客户端操作系统版本
            /// </summary>
            /// <returns></returns>
            public static string GetOSName()
            {
                if (HttpContext.Current == null || HttpContext.Current.Request == null || HttpContext.Current.Request.UserAgent == null)
                { return "未知"; }
    
                var userAgent = HttpContext.Current.Request.UserAgent.ToLower();
    
                if (userAgent.Contains("android"))
                { return "Android"; }
                else if (userAgent.Contains("mac os x"))
                { return "ios"; }
                else if (userAgent.Contains("windows phone"))
                { return "Windows Phone"; }
                else if (userAgent.Contains("nt 10.0"))
                { return "Windows 10"; }
                else if (userAgent.Contains("NT 6.3"))
                { return "Windows8.1"; }
                else if (userAgent.Contains("NT 6.2"))
                { return "Windows8"; }
                else if (userAgent.Contains("nt 6.1"))
                { return "Windows 7"; }
                else if (userAgent.Contains("nt 6.0"))
                { return "Windows Vista/Server 2008"; }
                else if (userAgent.Contains("nt 5.2"))
                { return "Windows Server 2003"; }
                else if (userAgent.Contains("nt 5.1"))
                { return "Windows XP"; }
                else if (userAgent.Contains("nt 5"))
                { return "Windows 2000"; }
                else if (userAgent.Contains("nt 4"))
                { return "Windows NT4"; }
                else if (userAgent.Contains("me"))
                { return "Windows Me"; }
                else if (userAgent.Contains("98"))
                { return "Windows 98"; }
                else if (userAgent.Contains("95"))
                { return "Windows 95"; }
                else if (userAgent.Contains("mac"))
                { return "Mac"; }
                else if (userAgent.Contains("unix"))
                { return "UNIX"; }
                else if (userAgent.Contains("linux"))
                { return "Linux"; }
                else if (userAgent.Contains("sunos"))
                { return "SunOS"; }
    
                return "未知";
            }
    
            /// <summary>
            /// 获取浏览器名称
            /// </summary>
            /// <returns></returns>
            public static string GetBrowser()
            {
                if (HttpContext.Current == null || HttpContext.Current.Request == null || HttpContext.Current.Request.UserAgent == null)
                { return "未知"; }
    
                var userAgent = HttpContext.Current.Request.UserAgent.ToLower();
    
                if (userAgent.Contains("opera/ucweb"))
                { return "UC Opera"; }
                else if (userAgent.Contains("openwave/ ucweb"))
                { return "UCOpenwave"; }
                else if (userAgent.Contains("ucweb"))
                { return "UC"; }
                else if (userAgent.Contains("360se"))
                { return "360"; }
                else if (userAgent.Contains("metasr"))
                { return "搜狗"; }
                else if (userAgent.Contains("maxthon"))
                { return "遨游"; }
                else if (userAgent.Contains("the world"))
                { return "世界之窗"; }
                else if (userAgent.Contains("tencenttraveler") || userAgent.Contains("qqbrowser"))
                { return "腾讯"; }
                else if (userAgent.Contains("chrome"))
                { return "Chrome"; }
                else if (userAgent.Contains("safari"))
                { return "safari"; }
                else if (userAgent.Contains("firefox"))
                { return "Firefox"; }
                else if (userAgent.Contains("opera"))
                { return "Opera"; }
                else if (userAgent.Contains("msie"))
                { return "IE"; }
                else
                { return System.Web.HttpContext.Current.Request.Browser.Browser; }
            }
    
            /// <summary>
            /// 取得客户端IP
            /// </summary>
            /// <returns></returns>
            public static string GetClientIp()
            {
                var result = "0.0.0.0";
    
                if (HttpContext.Current == null && HttpContext.Current.Request == null)
                { return result; }
    
                result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (result == null || result == String.Empty)
                {
                    result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }
                if (result == null || result == String.Empty)
                {
                    result = HttpContext.Current.Request.UserHostAddress;
                }
                if (result == null || result == String.Empty)
                {
                    result = "0.0.0.0";
                }
    
                return result;
            }
  • 相关阅读:
    友盟上报 IOS
    UTF8编码
    Hill加密算法
    Base64编码
    Logistic Regression 算法向量化实现及心得
    152. Maximum Product Subarray(中等, 神奇的 swap)
    216. Combination Sum III(medium, backtrack, 本类问题做的最快的一次)
    77. Combinations(medium, backtrack, 重要, 弄了1小时)
    47. Permutations II(medium, backtrack, 重要, 条件较难思考)
    3.5 find() 判断是否存在某元素
  • 原文地址:https://www.cnblogs.com/Aaxuan/p/10301926.html
Copyright © 2020-2023  润新知