我想看看如何利用MVP的WebClient和WebRequest类获取网页源代码,然后自己写一下。 当然,这是参考它的方法。
这次下载了visual web developer 2005 express
我按照上面的文章写了代码,
我的btn函数如下
字符串URL page=' ';
urlPage=UrlText.Text;
webrequest request=webrequest.create (URL page );
webresponse response=request.getresponse (;
stream resstream=response.getresponse stream (;
streamreader Sr=new streamreader (resstream,System.Text.Encoding.Default );
ContentHtml.Text=sr.ReadToEnd (;
resStream.Close (;
sr.Close (;
但是,我们发现在运行调试时发生了错误
A potentially dangerous Request.Form value was detected from the client
33558 www.Sina.com/requestvalidationhasdetectedapotentiallydangerousclientinputvalue, andprocessingoftherequesthasbeenaborted.thisvaluemayindicateanattempttocompromisethesecurityofyourapplication, suchas across-sitescriptingattack.youcandisablerequestvalidationbysettingvalidaterequest=falseinthepagedirectiveorinthecoriorinthecon itisstronglyrecommendedthatyourapplicationexplicitlycheckallinputsinthiscase.http://www.Sina.com/system.web.httprequestvalidationexception : apotentiallydangerousrequest.formvaluewasdetectedfromtheclient (
头戴
met . ' )。
我仔细查找了解决方法和问题的原因,原来是validaterequest的问题。 以下是网络参考连接。 写得很详细。 非常好啊
http://access911.net/index.asp? board=4recordid=71 fab 51e 15 DCE7F3
3358 support.Microsoft.com/default.aspx? scid=kb; en-us; 821343Product=aspnet
根据以上说明,解决方法又有三种。
1 .静态页面验证请求
%@ Page validateRequest='false' %
设置web.config
配置
系统. web
pages validateRequest='false' /
/系统. web
/configuration
3 .我个人认为最好的做法是采用Server.HtmlEncode这种方法
我做了如下更改
字符串URL page=' ';
urlPage=UrlText.Text;
webrequest request=webrequest.create (URL page );
webresponse response=request.getresponse (;
stream resstream=response.getresponse stream (;
streamreader Sr=new streamreader (resstream,System.Text.Encoding.Default );
content html.text=server.htmlencode (Sr.read to end ) );
resStream.Close (;
sr.Close (;
确定,执行,结束。 真好啊