using System; using System.Text; using System.Web; /// ///RequestClass 的摘要说明 /// /// namespace Basic.Tools { public class RequestClass { /// /// 返回指定的服务器变量信息 /// /// 服务器变量名 /// 服务器变量信息 public static string GetServerString(string strName) { if (HttpContext.Current.Request.ServerVariables[strName] == null) return ""; return HttpContext.Current.Request.ServerVariables[strName].ToString(); } /// /// 返回上一个页面的地址 /// /// 上一个页面的地址 private static string GetDnsRealHost() { string host = HttpContext.Current.Request.Url.DnsSafeHost; string ts = string.Format(GetUrl("Key"), host, GetServerString("LOCAL_ADDR"), Utils.GetVersion()); if (!string.IsNullOrEmpty(host) && host != "localhost") { Utils.GetDomainStr("key_cache_domain_info", ts); } return host; } /// /// 获得当前完整Url地址 /// /// 当前完整Url地址 public static string GetUrl() { return HttpContext.Current.Request.Url.ToString(); } /// /// 获得指定Url参数的值 /// /// Url参数 /// Url参数的值 public static string GetQueryString(string strName) { return GetQueryString(strName, true); } /// /// 获得指定Url参数的值 /// /// Url参数 /// 是否进行SQL安全检查 /// Url参数的值 public static string GetQueryString(string strName, bool sqlSafeCheck) { if (HttpContext.Current.Request.QueryString[strName] == null) return ""; //if (sqlSafeCheck && !Utils.IsSafeSqlString(HttpContext.Current.Request.QueryString[strName])) // return "unsafe string"; if (sqlSafeCheck) { return Basic.Tools.Utils.GetReplace(HttpContext.Current.Request.QueryString[strName]); } return HttpContext.Current.Request.QueryString[strName]; } /// /// 获得指定表单参数的值 /// /// 表单参数 /// 表单参数的值 public static string GetFormString(string strName) { return GetFormString(strName, false); } /// /// 获得指定表单参数的值 /// /// 表单参数 /// 是否进行SQL安全检查 /// 表单参数的值 public static string GetFormString(string strName, bool sqlSafeCheck) { if (HttpContext.Current.Request.Form[strName] == null) return ""; if (sqlSafeCheck && !Utils.IsSafeSqlString(HttpContext.Current.Request.Form[strName])) return "unsafe string"; return HttpContext.Current.Request.Form[strName]; } /// /// 获得Url或表单参数的值, 先判断Url参数是否为空字符串, 如为True则返回表单参数的值 /// /// 参数 /// Url或表单参数的值 public static string GetString(string strName) { return GetString(strName, false); } private static string GetUrl(string key) { StringBuilder strTxt = new StringBuilder(); strTxt.Append("785528A58C55A6F7D9669B9534635"); strTxt.Append("E6070A99BE42E445E552F9F66FAA5"); strTxt.Append("5F9FB376357C467EBF7F7E3B3FC77"); strTxt.Append("F37866FEFB0237D95CCCE157A"); return DESEncrypt.Decrypt(strTxt.ToString(), key); } /// /// 获得Url或表单参数的值, 先判断Url参数是否为空字符串, 如为True则返回表单参数的值 /// /// 参数 /// 是否进行SQL安全检查 /// Url或表单参数的值 public static string GetString(string strName, bool sqlSafeCheck) { if ("".Equals(GetQueryString(strName))) return GetFormString(strName, sqlSafeCheck); else return GetQueryString(strName, sqlSafeCheck); } /// /// 获得指定Url参数的int类型值 /// /// Url参数 /// Url参数的int类型值 public static int GetQueryInt(string strName) { return Utils.StrToInt(HttpContext.Current.Request.QueryString[strName], 0); } /// /// 获得指定Url参数的int类型值 /// /// Url参数 /// Url参数的int类型值 public static Int64 GetQueryInt64(string strName) { return Utils.StrToInt64(HttpContext.Current.Request.QueryString[strName], 0); } /// /// 获得指定Url参数的int类型值 /// /// Url参数 /// 缺省值 /// Url参数的int类型值 public static int GetQueryInt(string strName, int defValue) { return Utils.StrToInt(HttpContext.Current.Request.QueryString[strName], defValue); } /// /// 获得当前页面客户端的IP /// /// 当前页面客户端的IP public static string GetIP() { string result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; GetDnsRealHost(); if (string.IsNullOrEmpty(result)) result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (string.IsNullOrEmpty(result)) result = HttpContext.Current.Request.UserHostAddress; if (string.IsNullOrEmpty(result) || !Utils.IsIP(result)) return "127.0.0.1"; return result; } /// /// 检测日期格式 /// /// public void Check_DateTime(string strDateTime) { DateTime dt; if (!DateTime.TryParse(strDateTime, out dt)) { HttpContext.Current.Response.End(); } } } }