代理HTTP_VIA
/// <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; }
验证ip
/// <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])$");
时间: 2024-09-30 22:01:03