asp.net(vb.net)获取真实IP的函数
2010-11-03 00:00
821 查看
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>
相关文章推荐
- asp.net获取真实ip的方法
- asp.net获取真实ip的方法
- asp.net 获取客户端真实Ip
- asp.net 服务器获取真实IP
- asp.net获取客户真实ip非代理ip:
- 做了CDN获取用户真实IP的函数代码(PHP与Asp设置方式)
- 获取真实IP和代理IP (asp.net C#)
- ASP.NET获取用户端的真实IP
- asp.net获取真实ip
- asp.net 服务器获取真实IP
- ASP.NET获取真实IP
- 做了CDN获取用户真实IP的函数代码(PHP与Asp设置方式)
- Asp.Net使用代理IP远程获取数据
- ASP.NET获取客户端信息,获取客户端IP等等
- ASP.NET获取IP与MAC地址的方法
- asp.net获取客户端ip
- VB(ASP,VBScript) 获取数组长度函数
- ASP.NET获取IP的6种方法
- 获取客户端IPAsp.Net