码迷,mamicode.com
首页 > 其他好文 > 详细

获取客户端Ip

时间:2016-07-20 19:07:37      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

     /// <summary>  
        /// 获取客户端Ip  
        /// </summary>  
        /// <returns></returns>  
        public static string GetClientIp()
        {
            String clientIP = string.Empty;
            if (System.Web.HttpContext.Current != null)
            {
                clientIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (string.IsNullOrEmpty(clientIP) || (clientIP.ToLower() == "unknown"))
                {
                    clientIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_REAL_IP"];
                    if (string.IsNullOrEmpty(clientIP))
                    {
                        clientIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                    }
                }
                else
                {
                    clientIP = clientIP.Split(‘,‘)[0];
                }
            }
            return clientIP;
        }

获取客户端Ip

标签:

原文地址:http://www.cnblogs.com/XuPengLB/p/5689188.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!