就是说我想监控一个网站,我想用程序写:隔一断时间就访问一下,看它是否好使!
webrequest mywebrequest=webrequest.create(“一个无效链接的地址”);
mywebrequest.timeout=5000;
try
{
webresponse mywebresponse=mywebrequest.getresponse();
stream resstream = mywebresponse.getresponsestream();
streamreader sr = new streamreader(resstream, system.text.encoding.default);
this.label1.text = sr.readtoend();
resstream.close();
sr.close();
}
catch(system.net.webexception xx)
{
this.label1.text=xx.message;
}
如果label标签显示:无效的地址404错误说明链接无效
(如果有效的话label标签显示网页源代码)
==================================================================================
webrequest mywebrequest=webrequest.create(@”要试验的地址”);
mywebrequest.timeout=-1;
try
{
webresponse webresponse=webrequest.getresponse();
stream webstream = webresponse.getresponsestream();
webstream.close();
}
catch(system.net.webexception e)
{
//如果是得到了web服务器的响应,但是错误响应,则输出错误类型和其描述
if(e.status == webexceptionstatus.protocolerror)
{
console.writeline(“status code 222: {0}”, ((httpwebresponse)e.response).statuscode);
console.writeline(“status description 333: {0}”, ((httpwebresponse)e.response).statusdescription);
//console.writeline(e.message+”timeout”);
}
//如果是web服务器超时而没有响应,则输出超时信息
//超时值在上面的.wreq.timeout属性里设置
if(e.status == webexceptionstatus.timeout)
{
console.writeline(e.message+”timeout”);
}
//添加代码说明此链接不能访问
}