一、内容截取
这里分享一个比较方便的截取内容方法,亦可按自己需要修改
在C#中,我们想要首尾字符定位截取自己想要的内容,可以直接使用下面这个写好的方法。
不过,有时我们定位的起始位置并不止一个,不做任何处理的话,那么我们的程序就会截到错误的内容。
所以……
这里,注意观察代码【$*】中括号里的符号,这个是自定义的一种规则,可以解决不唯一的问题。
用法:输入内容中唯一标签,拼接上【】里的符号,最后拼接所要截取的起始标签
代码:
public string get_content_by_tag(string html, string start_tag, string end_tag)
{
string result = "";//返回结果
if (string.IsNullOrEmpty(html) || string.IsNullOrEmpty(end_tag))//内容为空或结尾标签为空,则返回空字符串
{
return "";
}
int start_index = 0;
int end_index = 0;
if (string.IsNullOrEmpty(start_tag))//无起始标签则默认索引为0
start_index = 0;
else
{ //特殊定位标签
if (start_tag.Contains("$*"))
{ //以$*分割标签(当你找的开始位置字符不唯一时,可以通过自己设置的$*规则定位唯一)
string[] taglist = start_tag.Split(new string[] { "$*" }, StringSplitOptions.RemoveEmptyEntries);
if (taglist.Length > 1 && html.Contains(taglist[0].ToString()))
{ //索引0标签的尾字符索引
int tag_index_0 = html.IndexOf(taglist[0].ToString()) + taglist[0].ToString().Length;
//截取起始索引
start_index = html.IndexOf(taglist[1].ToString(), tag_index_0);
if (taglist[1].ToString().Length > 0 && start_index < 0)//未找到
{
taglist = null;
return "";
}
else
{ //真正截取开始位置索引
start_index = start_index + taglist[1].ToString().Length;
}
taglist = null;
}
else
{
taglist = null;
return "";
}
}
else
{
if (!html.Contains(start_tag))//内容中没有找到,返回空字符串
return "";
else
{
start_index = html.IndexOf(start_tag);//获取首标签索引
start_index = start_index + start_tag.Length;//首标签尾的索引
}
}
if (start_index > html.Length)
return "";
else
{
end_index = html.IndexOf(end_tag, start_index);//以首标签索引开始的结尾标签的索引
if (end_index < start_index)
return "";
else
result = html.Substring(start_index, end_index - start_index);//将结果截取出来
}
}
return result;
}
最后,观察效果图
二、SQL注入处理
当某些参数传入我们的程序,会有sql注入风险,所有我们需要对传入的参数作处理
我们可以对即将传入的参数进行预处理
这里有一个写好的方法,拿来即用!
只要字符中含有 sql 关键字的、单引号的,通通都会去除
代码:
public static string SQLInjectFilter(string p_ParmValue)
{
string m_Result = "";//定义变量
//过滤词
const string SQL_INJECT_KEYWORD = @"'|execute\(|execute |exec\(|exec |create |select |insert |delete |truncate |update |drop |modify |rename |alter |declare | where | and | or |<script |<script>|</script>| \/*";
if (p_ParmValue.Length > 0)
{ //将含有的过滤词去除
m_Result = Regex.Replace(p_ParmValue, SQL_INJECT_KEYWORD, " ", RegexOptions.IgnoreCase);
}
return m_Result;
}
效果图: