An example of obtaining the real IP address of a remote client

Source: Internet
Author: User
Tags servervariables

An example of obtaining the real IP address of a remote client (for reference only ):

View code

1 public static string getip ()
2 {
3 string strip = string. empty;
4 // obtain the real IP address of the proxy user
5 // if it is not a proxy user, null is returned.
6 strip = httpcontext. Current. Request. servervariables ["http_x_forwarded_for"];
7 if (string. isnullorempty (strip ))
8 {
9 // obtain the IP Address requested by the remote client
10 strip = httpcontext. Current. Request. servervariables ["remote_addr"];
11}
12 if (string. isnullorempty (strip ))
13 {
14 // obtain the IP address of the remote client host
15 strip = httpcontext. Current. Request. userhostaddress;
16}
17 if (string. isnullorempty (strip )&&! Isip (strip ))
18 {
19 Return "0.0.0.0 ";
20}
21
22 return strip;
23}
24
25 public static bool isip (string strip)
26 {
27 // verify the IP Format
28 return RegEx. ismatch (strip, @ "^ (2 [0-4] \ d | 25 [0-5] | [01]? \ D ?) \.) {3} (2 [0-4] \ d | 25 [0-5] | [01]? \ D ?) $ ");
29}

--------------------------------------------------------- Welcome to learn and exchange ----------------------------------------------------------

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.