ASP.NET获取IP的6种方法
2009-02-13 15:42
274 查看
在ASP中使用
Request.ServerVariables("REMOTE_ADDR") 来取得客户端的IP地址,但如果客户端是使用代理服务器来访问,那取到的就是代理服务器的IP地址,而不是真正的客户端IP地址。
要想透过代理服务器取得客户端的真实IP地址,就要使用 Request.ServerVariables("HTTP_X_FORWARDED_FOR") 来读取。
不过要注意的事,并不是每个代理服务器都能用 Request.ServerVariables("HTTP_X_FORWARDED_FOR") 来读取客户端的真实 IP,有些用此方法读取到的仍然是代理服务器的IP。
还有一点需要注意的是:如果客户端没有通过代理服务器来访问,那么用 Request.ServerVariables ("HTTP_X_FORWARDED_FOR") 取到的值将是空的。因此,如果要在程序中使用此方法,可以这样处理:
......
userip = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If userip = "" Then userip = Request.ServerVariables("REMOTE_ADDR")
......
//方法一
HttpContext.Current.Request.UserHostAddress;
//方法二
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
//方法三
string strHostName = System.Net.Dns.GetHostName();
string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
//方法四(无视代理)
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
客户端:
//方法五
var ip = '<!--#echo var="REMOTE_ADDR"-->';
alert("Your IP address is "+ip);
//方法六(无视代理)
function GetLocalIPAddress()
if(Context.Request.ServerVariables["HTTP_VIA"]!=null) // 服务器, using proxy
else//如果没有使用代理服务器或者得不到客户端的ip not using proxy or can't get the Client IP
备注:
1. 有些代理是不会发给我们真实IP地址的
2. 有些客户端会因为“header_access deny”的安全设置而不发给我们IP
ServerVariables变量说明
Request.ServerVariables("REMOTE_ADDR") 来取得客户端的IP地址,但如果客户端是使用代理服务器来访问,那取到的就是代理服务器的IP地址,而不是真正的客户端IP地址。
要想透过代理服务器取得客户端的真实IP地址,就要使用 Request.ServerVariables("HTTP_X_FORWARDED_FOR") 来读取。
不过要注意的事,并不是每个代理服务器都能用 Request.ServerVariables("HTTP_X_FORWARDED_FOR") 来读取客户端的真实 IP,有些用此方法读取到的仍然是代理服务器的IP。
还有一点需要注意的是:如果客户端没有通过代理服务器来访问,那么用 Request.ServerVariables ("HTTP_X_FORWARDED_FOR") 取到的值将是空的。因此,如果要在程序中使用此方法,可以这样处理:
......
userip = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If userip = "" Then userip = Request.ServerVariables("REMOTE_ADDR")
......
//方法一
HttpContext.Current.Request.UserHostAddress;
//方法二
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
//方法三
string strHostName = System.Net.Dns.GetHostName();
string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
//方法四(无视代理)
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
客户端:
//方法五
var ip = '<!--#echo var="REMOTE_ADDR"-->';
alert("Your IP address is "+ip);
//方法六(无视代理)
function GetLocalIPAddress()
if(Context.Request.ServerVariables["HTTP_VIA"]!=null) // 服务器, using proxy
else//如果没有使用代理服务器或者得不到客户端的ip not using proxy or can't get the Client IP
备注:
1. 有些代理是不会发给我们真实IP地址的
2. 有些客户端会因为“header_access deny”的安全设置而不发给我们IP
ServerVariables变量说明
相关文章推荐
- ASP.NET获取IP的6种方法
- ASP.NET获取IP的6种方法
- ASP.NET获取IP的6种方法
- ASP.NET获取IP的6种方法
- ASP.NET获取IP的6种方法
- ASP.NET获取IP的6种方法 【转】
- ASP.NET获取IP的6种方法
- ASP.NET获取IP的6种方法
- ASP.NET获取IP的6种方法
- ASP.NET获取IP的6种方法
- ASP.NET获取IP的6种方法
- ASP.NET获取IP的6种方法
- ASP.NET获取IP的6种方法 ( 转)
- ASP.NET获取IP的6种方法
- ASP.NET获取IP的6种方法
- asp.net 获取ip的6种方法
- Asp,net获取IP的方法
- ASP.NET获取IP与MAC地址的方法
- ASP.NET获取IP的几种方法
- ASP.NET获取IP及电脑名等信息的简单方法+通用类文件源码