2013-12-02|2219阅|作者:伊景帆|举报 摘要:获取外网的IP地址
public static string getIp()
{
// 穿过代理服务器取远程用户真实IP地址
string Ip = string.Empty;
if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
{
if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] == null)
{
if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"] != null)
Ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"].ToString();
else
if (System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] != null)
Ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
else
Ip = "127.0.0.1";
}
else
Ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}
else if (System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] != null)
{
Ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
}
else
{
Ip = "127.0.0.1";
}
return Ip;
}