vb.net获取真实IP的函数
本文导语: vb.net获取真实IP的函数,供大家学习参考。 aspx vb.net获取真实IP的函数: 代码如下: 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 CheckI...
vb.net获取真实IP的函数,供大家学习参考。
aspx vb.net获取真实IP的函数:
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
完整的测试页:
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
Untitled Page