服务器之家

服务器之家 > 正文

asp.net(vb.net)获取真实IP的函数

时间:2019-09-10 11:37     来源/作者:asp.net教程网

aspx vb.net获取真实IP的函数如下: 

复制代码代码如下:


<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> 


完整的测试页面: 

复制代码代码如下:


<%@ 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> 

标签:

相关文章

热门资讯

玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情 2019-06-22
抖音撒撒累累是什么歌 撒撒累累张艺兴歌曲名字
抖音撒撒累累是什么歌 撒撒累累张艺兴歌曲名字 2019-06-05
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-07-24
叠猫猫队长可以退出吗 叠猫猫队长怎么换队伍
叠猫猫队长可以退出吗 叠猫猫队长怎么换队伍 2019-06-05
返回顶部