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}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.