体验: 代码如下:
1 using System; 2 using System.Text; 3 using System.Text.RegularExpressions; 4 using System.Web; 5 6 namespace HoverTree.HoverTreeFrame.HvtNet 7 { 8 public class HoverTreeIP 9 {10 ///11 /// 获取真实IP12 /// 13 ///14 public static string GetHoverTreeIp()15 { //http://tool.hovertree.com/info/ip/16 string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];17 if (null == result || result == String.Empty)18 {19 result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];20 }21 if (null == result || result == String.Empty)22 {23 result = HttpContext.Current.Request.UserHostAddress;24 }25 return result;26 }27 28 public static bool HvtIsIP(string ip)29 {30 return Regex.IsMatch(ip, @"^((23[0-3]|1\d{2}|[1-9]\d|[1-9])\.)((25[0-5]|2[0-4]\d|1?\d{1,2})\.){2}((25[0-5]|2[0-4]\d|1?\d{1,2}))$") ;31 }32 33 /// 34 /// 把IP地址转为整数 hovertree.com35 /// 36 /// 37 ///38 public static long HvtIpToLong(string ip)39 {40 char[] separator = new char[] { '.' };41 string[] items = ip.Split(separator);42 return long.Parse(items[0]) << 2443 | long.Parse(items[1]) << 1644 | long.Parse(items[2]) << 845 | long.Parse(items[3]);46 }47 48 /// 49 /// 把整数转为IP 何问起50 /// 51 /// 52 ///53 public static string HvtLongToIp(long ipLong)54 { //http://hovertree.com/hvtart/bjae/cn5qrmxw.htm55 StringBuilder sb = new StringBuilder();56 sb.Append((ipLong >> 24) & 0xFF).Append(".");57 sb.Append((ipLong >> 16) & 0xFF).Append(".");58 sb.Append((ipLong >> 8) & 0xFF).Append(".");59 sb.Append(ipLong & 0xFF);60 return sb.ToString();61 }62 }
类的代码将发布在HoverTreeCMS项目中。
ASP.NET开源CMS
开发技术文章收集