The function for aspx vb.net to obtain the real IP address is 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") Is 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") Is 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") Is 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") Is 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>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> Untitled Page </title>
</Head>
<Body>
<%
Dim client_ip As String = get_cli_ip ()
System. Web. HttpContext. Current. Response. Write (client_ip)
%>
</Body>
</Html>