获取客户端IP

 1   /// <summary>
 2         /// 获取用户IP地址
 3         /// </summary>
 4         /// <returns></returns>
 5         public static string GetClientIP()
 6         {
 7 
 8             string user_IP = string.Empty;
 9             if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
10             {
11                 if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
12                 {
13                     user_IP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
14                 }
15                 else
16                 {
17                     user_IP = System.Web.HttpContext.Current.Request.UserHostAddress;
18                 }
19             }
20             else
21             {
22                 user_IP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
23             }
24             return user_IP;
25         }
26  

 

posted @ 2013-07-23 11:04  mushishi  阅读(228)  评论(0编辑  收藏  举报