我编写了一个简单的控制台应用程序,通过检查始终有效的链接来检查我的网站是否正常运行。问题是它前几天宕机了,它没有通知我,它有错误:
system.net.webexception 操作在 system.net.httpwebrequest.getresponse 处超时
我将超时设置为 15000 毫秒,所以 15 秒。我在请求时查看了萤火虫,它说请求被中止,我没有看到状态码。但我想知道为什么我在设置超时时会得到这个期望?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
WebRequest request = WebRequest.Create("http://MYSITE.com/A/P/LIB/22?encoding=UTF-8&b=100");
request.Timeout = 15000;
SmtpClient mailserver = null;
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response == null || response.StatusCode != HttpStatusCode.OK)
{
MailMessage contactMsg = new MailMessage();
contactMsg.To.Add(new MailAddress("abc123@mysite.com"));
contactMsg.From = new MailAddress("donotreply@mysite.com");
contactMsg.Subject = "Site is not responding!";
contactMsg.IsBodyHtml = true;
contactMsg.Body = "<html><body><h1>The Site is not responding</h1> " +
"Please check the server. <br /><br />Thank You</body></html>";
mailserver = new SmtpClient("smtp.mysite.com", 25);
//Setup email message
try
{
mailserver.Send(contactMsg);
mailserver.Dispose();
}
catch (Exception exc)
{
Console.WriteLine(exc.ToString());
mailserver.Dispose();
}
Console.WriteLine("Site is Down!");
}
else
{
Console.WriteLine("Site is Up!");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
mailserver.Dispose();
}
}
}
}