问题:<a href="shownews.aspx?id=3367">显示这个新闻</a>接收页面,shownews.aspx中如何判断Request["id"]这个值是数值类型,如果不是数值类型就弹出框提示:大哥,请不要提交非法数据!我这样做就是为了防止有人在shownews.aspx?id=3367后面加一个',这样的字符来进行漏洞测试,进行攻击请各位大哥给个方法或源码,要能通过!谢谢了!急用!
调试欢乐多
{
int.Parse(Request["id"].ToString());
string str = "this a number";
}
catch
{
错误处理
}
Convert.ToInt16就可以了
在web.config统一设定出错页面
{
Response.Write("<script>alert('大哥,请不要提交非法数据!');history.go(-1);</scirpt>");
}------------------------------------------------------public static bool IsNumber(string InStr)
{
bool IsNum = true;
string tmpStr;
for(int i=0;i<=InStr.Length-1;i++)
{
tmpStr=InStr.Substring(i,1);
if(!Char.IsNumber(tmpStr,0))
{
IsNum = false;
break;
}
}
return IsNum;
}
public static bool isIntString(string strInt,string msg){
if(!Regex.IsMatch(strInt,@"^[\d\-]+$",RegexOptions.IgnoreCase)){
CheckAgent.info = msg + "无效整数";
return false;
}else{
return true;
}
}
然后你可以调用此函数,判断是否合法的字符串,如是合法,则返回true,如不舍法你就可以返回错误消息如
if(!CheckAgent.isIntString(Request["id"].ToString(),"ID值")){
showInfo(CheckAgent.info);
return ...
}