c# 获取客户端IP

2021-07-09 07:07

阅读:694

标签:string   except   ESS   returns   class   ||   try   获取客户端ip   客户   

        /// 
        /// 获得当前页面客户端的IP
        /// 
        /// 当前页面客户端的IP
        public static string GetIP()
        {
            try
            {
                string result = String.Empty;

                result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (string.IsNullOrEmpty(result))
                {
                    result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }

                if (string.IsNullOrEmpty(result))
                {
                    result = HttpContext.Current.Request.UserHostAddress;
                }

                if (string.IsNullOrEmpty(result) || !Utils.IsIP(result))
                {
                    return "127.0.0.1";
                }

                return result;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.Write(ex.Message);
                return HttpContext.Current.Request.UserHostAddress;
            }
        }

 

c# 获取客户端IP

标签:string   except   ESS   returns   class   ||   try   获取客户端ip   客户   

原文地址:https://www.cnblogs.com/yuanzijian-ruiec/p/9715555.html


评论


亲,登录后才可以留言!