方法一:
string user_ip;
user_ip = Request.ServerVariables.Get("REMOTE_ADDR").ToString();
//<==> user_ip = Request.ServerVariables["REMOTE_ADDR"].ToString();
Response.Write(user_ip);
方法二:
Response.Write (Request.UserHostAddress);
或者:
Response.Write (Request.UserHostName);
以上有时并不准确,因为有的使用代理服务器,那样的话,取的将是代理服务器的ip地址!
下面是做判断的:
if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] == null)
Response.Write (Request.ServerVariables["REMOTE_ADDR"].ToString());
else
Response.Write (Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString());
string user_ip;
user_ip = Request.ServerVariables.Get("REMOTE_ADDR").ToString();
//<==> user_ip = Request.ServerVariables["REMOTE_ADDR"].ToString();
Response.Write(user_ip);
方法二:
Response.Write (Request.UserHostAddress);
或者:
Response.Write (Request.UserHostName);
以上有时并不准确,因为有的使用代理服务器,那样的话,取的将是代理服务器的ip地址!
下面是做判断的:
if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] == null)
Response.Write (Request.ServerVariables["REMOTE_ADDR"].ToString());
else
Response.Write (Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString());