C # method for obtaining IP addresses and integer IP addresses,
Experience: http://tool.hovertree.com/info/ip/
The Code is as follows:
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 /// <summary> 11 /// obtain the real IP12 /// </summary> 13 /// <returns> </returns> 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 // <summary> 34 // convert the IP address to an integer hovertree. com35 // </summary> 36 // <param name = "ip"> </param> 37 // <returns> </returns> 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 // <summary> 49 // when I convert the integer into an IP address, ask 50 /// </summary> 51 /// <param name = "ipLong"> </param> 52 // <returns> </returns> 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}
The class code will be published in the HoverTreeCMS project.
ASP. NET Open Source CMS http://www.cnblogs.com/sosoft/p/cms.html
Development Technology Article collection http://www.cnblogs.com/sosoft/p/kaifajishu.html