欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net 生成静态时的过滤viewstate的实现方法

程序员文章站 2024-03-09 18:01:53
复制代码 代码如下:public static string getsourcetextbyurl(string url) { webrequest request = w...
复制代码 代码如下:

public static string getsourcetextbyurl(string url)
{
webrequest request = webrequest.create(url);
request.timeout = 200000;//20秒超时
webresponse response = request.getresponse();
stream resstream = response.getresponsestream();
streamreader sr = new streamreader(resstream);
string tempstr = sr.readtoend();

regex r1 = new regex("<input type=\"hidden\" name=\"__eventtarget\".*/>", regexoptions.ignorecase);
regex r2 = new regex("<input type=\"hidden\" name=\"__eventargument\".*/>", regexoptions.ignorecase);
regex r3 = new regex("<input type=\"hidden\" name=\"__viewstate\".*/>", regexoptions.ignorecase);
//过滤<form>代码
regex r4 = new regex("<form name=\"aspnetform\".*id=\"aspnetform\">", regexoptions.ignorecase);
regex r5 = new regex("</form>");
tempstr = r1.replace(tempstr, "");
tempstr = r2.replace(tempstr, "");
tempstr = r3.replace(tempstr, "");
tempstr = r4.replace(tempstr, "");
tempstr = r5.replace(tempstr, "");

return tempstr;
}