• .Net后台获取客户端信息


    1.获取浏览器版本号

    1 /// <summary>
    2         /// 获取浏览器版本号
    3         /// </summary>
    4         /// <returns></returns>
    5         public static string GetBrowser()
    6         {
    7             HttpBrowserCapabilities bc = HttpContext.Current.Request.Browser;
    8             return bc.Browser + bc.Version;
    9         }

    2.获取操作系统版本号

     1 /// <summary>
     2         /// 获取操作系统版本号
     3         /// </summary>
     4         /// <returns></returns>
     5         public static string GetOSVersion()
     6         {
     7             //UserAgent 
     8             var userAgent = HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"];
     9 
    10             var osVersion = "未知";
    11 
    12             if (userAgent.Contains("NT 6.1"))
    13             {
    14                 osVersion = "Windows 7";
    15             }
    16             else if (userAgent.Contains("NT 6.0"))
    17             {
    18                 osVersion = "Windows Vista/Server 2008";
    19             }
    20             else if (userAgent.Contains("NT 5.2"))
    21             {
    22                 osVersion = "Windows Server 2003";
    23             }
    24             else if (userAgent.Contains("NT 5.1"))
    25             {
    26                 osVersion = "Windows XP";
    27             }
    28             else if (userAgent.Contains("NT 5"))
    29             {
    30                 osVersion = "Windows 2000";
    31             }
    32             else if (userAgent.Contains("NT 4"))
    33             {
    34                 osVersion = "Windows NT4";
    35             }
    36             else if (userAgent.Contains("Me"))
    37             {
    38                 osVersion = "Windows Me";
    39             }
    40             else if (userAgent.Contains("98"))
    41             {
    42                 osVersion = "Windows 98";
    43             }
    44             else if (userAgent.Contains("95"))
    45             {
    46                 osVersion = "Windows 95";
    47             }
    48             else if (userAgent.Contains("Mac"))
    49             {
    50                 osVersion = "Mac";
    51             }
    52             else if (userAgent.Contains("Unix"))
    53             {
    54                 osVersion = "UNIX";
    55             }
    56             else if (userAgent.Contains("Linux"))
    57             {
    58                 osVersion = "Linux";
    59             }
    60             else if (userAgent.Contains("SunOS"))
    61             {
    62                 osVersion = "SunOS";
    63             }
    64             return osVersion;
    65         }

    3.获取客户端IP地址

     1 /// <summary>
     2         /// 获取客户端IP地址
     3         /// </summary>
     4         /// <returns></returns>
     5         public static string GetIP()
     6         {
     7             string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
     8             if (string.IsNullOrEmpty(result))
     9             {
    10                 result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
    11             }
    12             if (string.IsNullOrEmpty(result))
    13             {
    14                 result = HttpContext.Current.Request.UserHostAddress;
    15             }
    16             if (string.IsNullOrEmpty(result))
    17             {
    18                 return "0.0.0.0";
    19             }
    20             return result;
    21         }

    4.取客户端真实IP

     1 ///  <summary>  
     2         ///  取得客户端真实IP。如果有代理则取第一个非内网地址  
     3         ///  </summary>  
     4         public static string GetIPAddress
     5         {
     6             get
     7             {
     8                 var result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
     9                 if (!string.IsNullOrEmpty(result))
    10                 {
    11                     //可能有代理  
    12                     if (result.IndexOf(".") == -1)        //没有“.”肯定是非IPv4格式  
    13                         result = null;
    14                     else
    15                     {
    16                         if (result.IndexOf(",") != -1)
    17                         {
    18                             //有“,”,估计多个代理。取第一个不是内网的IP。  
    19                             result = result.Replace("  ", "").Replace("'", "");
    20                             string[] temparyip = result.Split(",;".ToCharArray());
    21                             for (int i = 0; i < temparyip.Length; i++)
    22                             {
    23                                 if (IsIPAddress(temparyip[i])
    24                                         && temparyip[i].Substring(0, 3) != "10."
    25                                         && temparyip[i].Substring(0, 7) != "192.168"
    26                                         && temparyip[i].Substring(0, 7) != "172.16.")
    27                                 {
    28                                     return temparyip[i];        //找到不是内网的地址  
    29                                 }
    30                             }
    31                         }
    32                         else if (IsIPAddress(result))  //代理即是IP格式  
    33                             return result;
    34                         else
    35                             result = null;        //代理中的内容  非IP,取IP  
    36                     }
    37 
    38                 }
    39 
    40                 string IpAddress = (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null && HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != String.Empty) ? HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] : HttpContext.Current.Request.ServerVariables["HTTP_X_REAL_IP"];
    41 
    42                 if (string.IsNullOrEmpty(result))
    43                     result = HttpContext.Current.Request.ServerVariables["HTTP_X_REAL_IP"];
    44 
    45                 if (string.IsNullOrEmpty(result))
    46                     result = HttpContext.Current.Request.UserHostAddress;
    47 
    48                 return result;
    49             }
    50         }

    5.判断是否是IP格式

     1 ///  <summary>
     2         ///  判断是否是IP地址格式  0.0.0.0
     3         ///  </summary>
     4         ///  <param  name="str1">待判断的IP地址</param>
     5         ///  <returns>true  or  false</returns>
     6         public static bool IsIPAddress(string str1)
     7         {
     8             if (string.IsNullOrEmpty(str1) || str1.Length < 7 || str1.Length > 15) return false;
     9 
    10             const string regFormat = @"^d{1,3}[.]d{1,3}[.]d{1,3}[.]d{1,3}$";
    11 
    12             var regex = new Regex(regFormat, RegexOptions.IgnoreCase);
    13             return regex.IsMatch(str1);
    14         }

    6.获取公网IP

     1 /// <summary>
     2         /// 获取公网IP
     3         /// </summary>
     4         /// <returns></returns>
     5         public static string GetNetIP()
     6         {
     7             string tempIP = "";
     8             try
     9             {
    10                 System.Net.WebRequest wr = System.Net.WebRequest.Create("http://city.ip138.com/ip2city.asp");
    11                 System.IO.Stream s = wr.GetResponse().GetResponseStream();
    12                 System.IO.StreamReader sr = new System.IO.StreamReader(s, System.Text.Encoding.GetEncoding("gb2312"));
    13                 string all = sr.ReadToEnd(); //读取网站的数据
    14 
    15                 int start = all.IndexOf("[") + 1;
    16                 int end = all.IndexOf("]", start);
    17                 tempIP = all.Substring(start, end - start);
    18                 sr.Close();
    19                 s.Close();
    20             }
    21             catch
    22             {
    23                 if (System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList.Length > 1)
    24                     tempIP = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList[1].ToString();
    25                 if (string.IsNullOrEmpty(tempIP))
    26                     return GetIP();
    27             }
    28             return tempIP;
    29         }
  • 相关阅读:
    JVM活学活用——GC算法 垃圾收集器
    JVM活学活用——类加载机制
    JVM活学活用——Jvm内存结构
    优化springboot
    Java基础巩固计划
    Java自定义注解
    记一次内存溢出的分析经历
    redis学习笔记-redis的安装
    记一次线程池调优经历
    Python中关于split和splitext的差别和运用
  • 原文地址:https://www.cnblogs.com/huangtaiyi/p/10789385.html
Copyright © 2020-2023  润新知