using System; using System.Text; using System.IO; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Text.RegularExpressions; namespace basic { public class Utils { /// /// 获取AppSeting字符串函数 /// /// 所要获取的字符串 /// public static string GetAppSeting(string key) { if (key == null || key == "") return ""; string outstr = ""; outstr = System.Configuration.ConfigurationManager.AppSettings[key]; if (outstr == null || outstr == "") return ""; return outstr; } /// /// 编码转换UnicodeToGB,如,\u548c换成中文汉字 /// /// 输入内容 /// 输出内容 private string UnicodeToGB(string content) { Regex objRegex = new Regex("&#(?[\\d]{5});", RegexOptions.IgnoreCase); Match objMatch = objRegex.Match(content); System.Text.StringBuilder sb = new System.Text.StringBuilder(content); while (objMatch.Success) { string code = Convert.ToString(Convert.ToInt32(objMatch.Result("${UnicodeCode}")), 16); byte[] array = new byte[2]; array[0] = (byte)Convert.ToInt32(code.Substring(2), 16); array[1] = (byte)Convert.ToInt32(code.Substring(0, 2), 16); sb.Replace(objMatch.Value, System.Text.Encoding.Unicode.GetString(array)); objMatch = objMatch.NextMatch(); } return sb.ToString(); } /// /// 获取字符传的位数,位数不能少于1 /// public static string getstr(string str1, int leng) { string tmp = str1; if (leng > 0) { if (tmp.Length > leng) { tmp.Remove(leng); tmp = tmp + "..."; } } return tmp; } /// /// 截取字符串函数 /// /// 所要截取的字符串 /// 截取字符串的长度 /// public static string GetSubString(string Str, int Num) { if (Str == null || Str == "") return ""; string outstr = ""; int n = 0; foreach (char ch in Str) { n += System.Text.Encoding.Default.GetByteCount(ch.ToString()); if (n > Num) break; else outstr += ch; } return outstr; } /// /// 截取字符串函数 /// /// 所要截取的字符串 /// 截取字符串的长度 /// 截取字符串后省略部分的字符串 /// public static string GetSubString(string Str, int Num, string LastStr) { return (Str.Length > Num) ? Str.Substring(0, Num) + LastStr : Str; } /// /// MD5加密字符串处理 /// /// 加密是16位还是32位;如果为true为16位 /// 待加密码字符串 /// public static string MD5(string Input, bool Half) { string output = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Input, "MD5").ToLower(); if (Half)//16位MD5加密(取32位加密的9~25字符) output = output.Substring(8, 16); return output; } public static string MD5(string Input) { return MD5(Input, true); } /// /// 过滤Sql /// /// /// public static string FilterSql(string sql) { sql = sql.Replace("'", "''"); return sql; } /// /// 分析用户请求是否正常 /// /// 传入用户提交数据 /// 返回是否含有SQL注入式攻击代码 private bool ProcessSqlStr(string Str) { bool ReturnValue = true; try { if (Str.Trim() != "") { string SqlStr = "and |exec |insert |select |delete |update |count |* |chr |mid |master |truncate |char |declare"; string[] anySqlStr = SqlStr.Split('|'); foreach (string ss in anySqlStr) { if (Str.ToLower().IndexOf(ss) >= 0) { ReturnValue = false; break; } } } } catch { ReturnValue = false; } return ReturnValue; } /// /// 执行一个JS语句 /// /// 要执行的语句 public static void ExecuteJs(string sentence) { HttpContext.Current.Response.Write(""); } /// /// 判断对象是否为Int32类型的数字 /// /// /// public static bool IsNumeric(object expression) { if (expression != null) { return IsNumeric(expression.ToString()); } return false; } /// /// 判断对象是否为Int32类型的数字 /// /// /// public static bool IsNumeric(string expression) { if (expression != null) { string str = expression; if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*[.]?[0-9]*$")) { if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1')) { return true; } } } return false; } /// /// 是否为Double类型 /// /// /// public static bool IsDouble(object expression) { if (expression != null) { return Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$"); } return false; } /// /// string型转换为bool型 /// /// 要转换的字符串 /// 缺省值 /// 转换后的bool类型结果 public static bool StrToBool(object expression, bool defValue) { if (expression != null) { return StrToBool(expression, defValue); } return defValue; } /// /// string型转换为bool型 /// /// 要转换的字符串 /// 缺省值 /// 转换后的bool类型结果 public static bool StrToBool(string expression, bool defValue) { if (expression != null) { if (string.Compare(expression, "true", true) == 0) { return true; } else if (string.Compare(expression, "false", true) == 0) { return false; } } return defValue; } /// /// 将对象转换为Int32类型 /// /// 要转换的字符串 /// 缺省值 /// 转换后的int类型结果 public static int StrToInt(object expression, int defValue) { if (expression != null) { return StrToInt(expression.ToString(), defValue); } return defValue; } /// /// 将对象转换为Int32类型 /// /// 要转换的字符串 /// 缺省值 /// 转换后的int类型结果 public static int StrToInt(string str, int defValue) { if (str == null) return defValue; if (str.Length > 0 && str.Length <= 11 && Regex.IsMatch(str, @"^[-]?[0-9]*$")) { if ((str.Length < 10) || (str.Length == 10 && str[0] == '1') || (str.Length == 11 && str[0] == '-' && str[1] == '1')) { return Convert.ToInt32(str); } } return defValue; } /// /// string型转换为float型 /// /// 要转换的字符串 /// 缺省值 /// 转换后的int类型结果 public static float StrToFloat(object strValue, float defValue) { if ((strValue == null)) { return defValue; } return StrToFloat(strValue.ToString(), defValue); } /// /// string型转换为float型 /// /// 要转换的字符串 /// 缺省值 /// 转换后的int类型结果 public static float StrToFloat(string strValue, float defValue) { if ((strValue == null) || (strValue.Length > 10)) { return defValue; } float intValue = defValue; if (strValue != null) { bool IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$"); if (IsFloat) { intValue = Convert.ToSingle(strValue); } } return intValue; } /// /// 判断给定的字符串数组(strNumber)中的数据是不是都为数值型 /// /// 要确认的字符串数组 /// 是则返加true 不是则返回 false public static bool IsNumericArray(string[] strNumber) { if (strNumber == null) { return false; } if (strNumber.Length < 1) { return false; } foreach (string id in strNumber) { if (!IsNumeric(id)) { return false; } } return true; } /// /// 写cookie值 /// /// 名称 /// 值 public static void WriteCookie(string strName, string strValue) { HttpCookie cookie = HttpContext.Current.Request.Cookies[strName]; if (cookie == null) { cookie = new HttpCookie(strName); } cookie.Value = strValue; HttpContext.Current.Response.AppendCookie(cookie); } /// /// 写cookie值 /// /// 名称 /// 值 public static void WriteCookie(string strName, string key, string strValue) { HttpCookie cookie = HttpContext.Current.Request.Cookies[strName]; if (cookie == null) { cookie = new HttpCookie(strName); } cookie[key] = strValue; HttpContext.Current.Response.AppendCookie(cookie); } /// /// 写cookie值 /// /// 名称 /// 值 /// 过期时间(分钟) public static void WriteCookie(string strName, string strValue, int expires) { HttpCookie cookie = HttpContext.Current.Request.Cookies[strName]; if (cookie == null) { cookie = new HttpCookie(strName); } cookie.Value = strValue; cookie.Expires = DateTime.Now.AddMinutes(expires); HttpContext.Current.Response.AppendCookie(cookie); } /// /// 读cookie值 /// /// 名称 /// cookie值 public static string GetCookie(string strName) { if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null) { return HttpContext.Current.Request.Cookies[strName].Value.ToString(); } return ""; } /// /// 读cookie值 /// /// 名称 /// cookie值 public static string GetCookie(string strName, string key) { if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null && HttpContext.Current.Request.Cookies[strName][key] != null) { return HttpContext.Current.Request.Cookies[strName][key].ToString(); } return ""; } /// /// 获得当前页面客户端的IP /// /// 当前页面客户端的IP public static string GetIP() { string result = String.Empty; result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (string.IsNullOrEmpty(result)) { result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } if (string.IsNullOrEmpty(result)) { result = HttpContext.Current.Request.UserHostAddress; } if (result == "127.0.0.1") { result = HttpContext.Current.Request.ServerVariables["HTTP_X_REAL_IP"]; if (result == "") { result = "127.0.0.1"; } } return result; } /// /// 是否为ip /// /// /// public static bool IsIP(string ip) { return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$"); } /// /// 判断是否时间格式 /// /// /// public static bool IsTime(string str) { bool bol = false; DateTime Dt = new DateTime(); if (DateTime.TryParse(str, out Dt)) { bol = true; } else { bol = false; } return bol; } /// /// 判断当前页面是否接收到了Post请求 /// /// 是否接收到了Post请求 public static bool IsPost() { return HttpContext.Current.Request.HttpMethod.Equals("POST"); } /// /// 判断当前页面是否接收到了Get请求 /// /// 是否接收到了Get请求 public static bool IsGet() { return HttpContext.Current.Request.HttpMethod.Equals("GET"); } /// /// 获得指定表单参数的值 /// /// 表单参数 /// 表单参数的值 public static string GetFileFullPath(string strName) { if (HttpContext.Current.Server.MapPath(strName) == null) { return ""; } return HttpContext.Current.Server.MapPath(strName); } /// /// 替换字符串中的空格 /// /// /// public static string ReplaceSpace(string strName) { return strName.Replace(" ", "-").Replace("&", "-"); } /// /// 获得指定表单参数的值 /// /// 表单参数 /// 表单参数的值 public static string GetFormString(string strName) { if (HttpContext.Current.Request.Form[strName] == null) { return ""; } return HttpContext.Current.Request.Form[strName]; } /// /// 获得指定Url参数的值 /// /// Url参数 /// Url参数的值 public static string GetQueryString(string strName) { if (HttpContext.Current.Request.QueryString[strName] == null) { return ""; } return HttpContext.Current.Request.QueryString[strName]; } /// /// 获得当前完整Url地址 /// /// 当前完整Url地址 public static string GetUrl() { return HttpContext.Current.Request.Url.ToString(); } /// /// 返回上一个页面的地址 /// /// 上一个页面的地址 public static string GetUrlReferrer() { string retVal = null; try { retVal = HttpContext.Current.Request.UrlReferrer.ToString(); } catch { } if (retVal == null) return ""; return retVal; } /// /// 过滤html标签 /// /// /// public static string FilterHtmlStr(string html) { System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" no[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"\]+\>", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"

", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"

", System.Text.RegularExpressions.RegexOptions.IgnoreCase); System.Text.RegularExpressions.Regex regex9 = new System.Text.RegularExpressions.Regex(@"<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase); html = regex1.Replace(html, ""); //过滤标记 html = regex2.Replace(html, ""); //过滤href=javascript: () 属性 html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件 html = regex4.Replace(html, ""); //过滤iframe html = regex5.Replace(html, ""); //过滤frameset html = regex6.Replace(html, ""); //过滤frameset html = regex7.Replace(html, ""); //过滤frameset html = regex8.Replace(html, ""); //过滤frameset html = regex9.Replace(html, ""); //html = html.Replace(" ", ""); html = html.Replace("", ""); html = html.Replace("", ""); return html; } ///

/// URL路径编码 /// /// public static string UrlEncode(string strHtml) { return HttpUtility.UrlEncode(strHtml, Encoding.Default); } /// /// URL路径解码 /// /// public static string UrlDecode(string strHtml) { return HttpUtility.UrlDecode(strHtml, Encoding.Default); } ///// ///// URL路径编码 ///// ///// //public static string UrlPathEncode(string strHtml) //{ // return HttpUtility.UrlPathEncode(strHtml); //} } }