C# 获取客户端IP
public string GetClientIP()
{
string sUserIP = string.Empty;
if (Request.ServerVariables["HTTP_VIA"] != null)
{
// 穿过代理服务器取远程用户真实IP地址
sUserIP = Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
if (string.IsNullOrEmpty(sUserIP))
sUserIP = HttpContext.Current.Request.UserHostAddress;
else
sUserIP = Request.ServerVariables["REMOTE_ADDR"].ToString();
}
else
{
if (string.IsNullOrEmpty(sUserIP))
sUserIP = HttpContext.Current.Request.UserHostAddress;
else
sUserIP = Request.ServerVariables["REMOTE_ADDR"].ToString();
}
return sUserIP;
}
{
string sUserIP = string.Empty;
if (Request.ServerVariables["HTTP_VIA"] != null)
{
// 穿过代理服务器取远程用户真实IP地址
sUserIP = Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
if (string.IsNullOrEmpty(sUserIP))
sUserIP = HttpContext.Current.Request.UserHostAddress;
else
sUserIP = Request.ServerVariables["REMOTE_ADDR"].ToString();
}
else
{
if (string.IsNullOrEmpty(sUserIP))
sUserIP = HttpContext.Current.Request.UserHostAddress;
else
sUserIP = Request.ServerVariables["REMOTE_ADDR"].ToString();
}
return sUserIP;
}