Visual C # IP related operation class, obtain the IP address of the current client, determine the IP Format, that is, check whether it is an IP address. For some basic operations on the IP address, welcome to C # for reference:
01 ///
02 // obtain the IP address of the client on the current page
03 ///
04 // IP address of the client on the current page
05 public static string GetIP ()
06 {
07 string result = String. Empty;
08 result = HttpContext. Current. Request. ServerVariables ["HTTP_X_FORWARDED_FOR"];
09 if (null = result | result = String. Empty)
10 {
11 result = HttpContext. Current. Request. ServerVariables ["REMOTE_ADDR"];
12}
13 if (null = result | result = String. Empty)
14 {
15 result = HttpContext. Current. Request. UserHostAddress;
16}
17 if (null = result | result = String. Empty |! IsIP (result ))
18 {
19 return "0.0.0.0 ";
20}
21 return result;
22}
23 ///
24 // whether it is an ip address
25 ///
26 ///
27 ///
28 public static bool IsIP (string ip)
29 {
30 return Regex. IsMatch (ip, @ "^ (2 [0-4] \ d | 25 [0-5] | [01]? \ D ?) \.) {3} (2 [0-4] \ d | 25 [0-5] | [01]? \ D ?) $ ");
31}