关于web请求中 获取真实IP

HttpRequest request = HttpContext.Current.Request;

                if (!string.IsNullOrEmpty(request.ServerVariables["HTTP_X_FORWARDED_FOR"]))
                {
                    //ip = GetCDNIP(request.ServerVariables["HTTP_ARR_IP"]);
                    if (string.IsNullOrEmpty(ip))
                    {
                        ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString().Split(',')[0].Trim();
                    }
                }
                if (string.IsNullOrEmpty(ip) || ip.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Length != 4)
                {
                    ip = GetArrIP();
                    if (!string.IsNullOrWhiteSpace(ip) && ip.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Length != 4)
                    {
                        ip = string.Empty;
                    }
                }
                //if (string.IsNullOrEmpty(ip))
                //{
                //    ip = GetCDNIP(null);
                //}
                if (string.IsNullOrEmpty(ip))
                {
                    ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }

                if (string.IsNullOrEmpty(ip))
                {
                    ip = request.UserHostAddress;
                }

  

posted @ 2016-09-14 09:44  破碎记忆  阅读(791)  评论(0编辑  收藏  举报