Sunshine, Pure Land
动物的趋光性

/// /// 获得用户的IP /// /// public static string GetIP() { //得到客户端IP if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)// 服务器 { //得到真实的客户端地址 return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString().Split(',')[0].Trim(); } else { return HttpContext.Current.Request.UserHostAddress; } } protected void btnSou_Click(object sender, EventArgs e) { GetIP(); } /// /// 用户的IP /// public static string UserIp { get { string text1 = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; switch (text1) { case null: case "": text1 = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; break; } if (!IsIp(text1)) { return "Unknown"; } return text1; } } public static bool IsIp(string s) { string text1 = @"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"; return Regex.IsMatch(s, text1); } public static string GetIPAddress() { string user_IP = string.Empty; if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null) { if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null) { user_IP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); } else { user_IP = System.Web.HttpContext.Current.Request.UserHostAddress; } } else { user_IP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString(); } return user_IP; }

posted on 2010-11-19 18:59  冰-阳光  阅读(246)  评论(0编辑  收藏  举报