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

获取客户端ip并用正则表达式验证

时间:2014-09-23 14:43:24      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   ar   for   div   sp   

代理HTTP_VIA

bubuko.com,布布扣
  /// <summary>
        /// 获得请求的ip
        /// </summary>
        /// <returns></returns>
        public static string GetIP()
        {
            string ip = string.Empty;
            if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
                ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
            else
                ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();

            if (string.IsNullOrEmpty(ip) || !ValidateHelper.IsIP(ip))
                ip = "127.0.0.1";
            return ip;
        }
View Code

验证ip

bubuko.com,布布扣
  /// <summary>
        /// 是否为IP
        /// </summary>
        public static bool IsIP(string s)
        {
            return _ipregex.IsMatch(s);
        }



 //IP正则表达式
        private static Regex _ipregex = new Regex(@"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$");
View Code

 

获取客户端ip并用正则表达式验证

标签:style   blog   http   color   os   ar   for   div   sp   

原文地址:http://www.cnblogs.com/lihongchen/p/3987926.html

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