C # determine the IP Format to obtain the IP address of the current client

Source: Internet
Author: User
Tags servervariables

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}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.