我用
string ip = Request.UserHostAddress;
获取客户端IP,可是在用本机测试时,得到的值为:::1
我的机器是Vista,无线上网,用的是默认的IE7浏览器不知道怎么才能得到正确的IP地址了,请赐教
string ip = Request.UserHostAddress;
获取客户端IP,可是在用本机测试时,得到的值为:::1
我的机器是Vista,无线上网,用的是默认的IE7浏览器不知道怎么才能得到正确的IP地址了,请赐教
Vista的机器没跑过, 无法模拟你的环境,按理来说可以获得客户端IP不保证能支持所有平台 你可以试试Request.ServerVariables["Remote_Addr"]
//http_x_forwarded_for
public static string GetClientIp()
{
string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (ip == null || ip == string.Empty || ip.ToLower().IndexOf("unknown") > -1)
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
else if (ip.IndexOf(",") > -1)
ip = ip.Substring(0, ip.IndexOf(","));
else if (ip.IndexOf(";") > -1)
ip = ip.Substring(0, ip.IndexOf(";"));
return ip;
}
# special IPv6 addresses
::1 localhost ipv6-localhost ipv6-loopback
我的系统是windows2008