C # to determine IP format to obtain current client IP

Source: Internet
Author: User
Tags httpcontext servervariables

Visual C # IP related Operations class, to obtain the current client IP, to determine the IP format, that is, detect whether the IP address, some of the basic operation of IP, welcome C # Novice Reference:

01///

02///gets the IP of the current page client

03///

04///The current page client's IP

05public static string GetIP ()

269

Modified string result = String.Empty;

result = httpcontext.current.request.servervariables["http_x_forwarded_for"];

if (Null = result | | = = String.Empty)

10 {

One result = httpcontext.current.request.servervariables["REMOTE_ADDR"];

12}

if (Null = result | | = = String.Empty)

14 {

result = HttpContext.Current.Request.UserHostAddress;

16}

if (Null = result | |) result = = String.Empty | |! IsIP (Result))

18 {

return "0.0.0.0";

20}

return result;

22}

23///

Whether the 24///is IP

25///

26///

27///

28public static bool IsIP (string IP)

099

return Regex.IsMatch (IP, @ "^" (2[0-4]d|25[0-5]|[ 01]?dd?). {3} (2[0-4]d|25[0-5]| [01]?dd?] $");

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.