asp.net (vb.net) Gets the real IP function

Source: Internet
Author: User
Tags exit end httpcontext integer net string split trim
The ASPX vb.net gets the real IP function as follows:
Copy CodeThe code is as follows:
<script runat= "Server" >
Public Function checkip (ByVal IP as String) as Boolean
Dim Pat as String = "^[0-9]{1,3}." [0-9] {1,3}. [0-9] {1,3}. [0-9] {1,3}$ "
Dim reg as Regex = New Regex (PAT)
If IP = "" Then
Checkip = False
Exit Function
End If
Checkip = Reg. IsMatch (IP)
End Function

Public Function get_cli_ip () as String
If Not (System.Web.HttpContext.Current.Request.ServerVariables ("http_client_ip") are nothing) and Checkip ( System.Web.HttpContext.Current.Request.ServerVariables ("http_client_ip")) = True) Then
GET_CLI_IP = System.Web.HttpContext.Current.Request.ServerVariables ("Http_client_ip")
Exit Function
ElseIf Not (System.Web.HttpContext.Current.Request.ServerVariables ("http_x_forwarded_for") are nothing) Then
Dim IPs () as String = Split (System.Web.HttpContext.Current.Request.ServerVariables ("Http_x_forwarded_for"), ",")
For i as Integer = 0 to IPs. Length-1
If Checkip (Trim (IPs (i)) = True Then
GET_CLI_IP = Trim (IPs (i))
Exit Function
End If
Next
End If
GET_CLI_IP = System.Web.HttpContext.Current.Request.ServerVariables ("REMOTE_ADDR")
End Function
</script>

Complete test page:
Copy CodeThe code is as follows:
<%@ Page language= "VB" autoeventwireup= "false" codefile= "Default.aspx.vb" inherits= "_default"%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<script runat= "Server" >
Public Function checkip (ByVal IP as String) as Boolean
Dim Pat as String = "^[0-9]{1,3}." [0-9] {1,3}. [0-9] {1,3}. [0-9] {1,3}$ "
Dim reg as Regex = New Regex (PAT)
If IP = "" Then
Checkip = False
Exit Function
End If
Checkip = Reg. IsMatch (IP)
End Function

Public Function get_cli_ip () as String
If Not (System.Web.HttpContext.Current.Request.ServerVariables ("http_client_ip") are nothing) and Checkip ( System.Web.HttpContext.Current.Request.ServerVariables ("http_client_ip")) = True) Then
GET_CLI_IP = System.Web.HttpContext.Current.Request.ServerVariables ("Http_client_ip")
Exit Function
ElseIf Not (System.Web.HttpContext.Current.Request.ServerVariables ("http_x_forwarded_for") are nothing) Then
Dim IPs () as String = Split (System.Web.HttpContext.Current.Request.ServerVariables ("Http_x_forwarded_for"), ",")
For i as Integer = 0 to IPs. Length-1
If Checkip (Trim (IPs (i)) = True Then
GET_CLI_IP = Trim (IPs (i))
Exit Function
End If
Next
End If
GET_CLI_IP = System.Web.HttpContext.Current.Request.ServerVariables ("REMOTE_ADDR")
End Function
</script>

<title>untitled page</title>
<body>
<%
Dim client_ip as String = Get_cli_ip ()
System.Web.HttpContext.Current.Response.Write (CLIENT_IP)
%>
</body>


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.