C# 控制台应用程序上的System.Net.WebException

C# 控制台应用程序上的System.Net.WebException,c#,asp.net,system.net.webexception,C#,Asp.net,System.net.webexception,我编写了一个简单的控制台应用程序来检查我的站点是否通过检查一个始终有效的链接来运行。问题是前几天它坏了,没有通知我,它有错误: system.net.webexception操作在处超时 system.net.httpwebrequest.getresponse 我将超时设置为15000毫秒,因此为15秒。当firebug发出请求时,我看了看它,它说请求被中止了,我没有看到状态码。但是我想知道为什么我在设置超时时会得到这样的期望 using System; using System.Collec

我编写了一个简单的控制台应用程序来检查我的站点是否通过检查一个始终有效的链接来运行。问题是前几天它坏了,没有通知我,它有错误:

system.net.webexception操作在处超时 system.net.httpwebrequest.getresponse

我将超时设置为15000毫秒,因此为15秒。当firebug发出请求时,我看了看它,它说请求被中止了,我没有看到状态码。但是我想知道为什么我在设置超时时会得到这样的期望

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();
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Net.Mail;
Net系统;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
WebRequest=WebRequest.Create(“http://MYSITE.com/A/P/LIB/22?encoding=UTF-8&b=100“;
请求超时=15000;
SmtpClient-mailserver=null;
尝试
{
HttpWebResponse=(HttpWebResponse)request.GetResponse();
if(response==null | | response.StatusCode!=HttpStatusCode.OK)
{
MailMessage contactMsg=新邮件();
contactMsg.To.Add(新邮件地址(“abc123@mysite.com"));
contactMsg.From=新邮件地址(“donotreply@mysite.com");
contactMsg.Subject=“站点没有响应!”;
contactMsg.IsBodyHtml=true;
contactMsg.Body=“网站没有响应”+
“请检查服务器。

谢谢”; mailserver=新的SmtpClient(“smtp.mysite.com”,25); //设置电子邮件 尝试 { mailserver.Send(contactMsg); mailserver.Dispose(); } 捕获(异常exc) { Console.WriteLine(exc.ToString()); mailserver.Dispose(); } 控制台。WriteLine(“站点已关闭!”); } 其他的 { 控制台。WriteLine(“站点已启动!”); } } 捕获(例外情况除外) { Console.WriteLine(例如ToString()); mailserver.Dispose(); } } } }
您的站点在15秒内没有响应,因此引发了
WebException
异常<代码>超时旨在引发异常

Timeout属性表示时间长度,以毫秒为单位, 直到请求超时并引发WebException。超时 属性仅影响使用GetResponse发出的同步请求 方法。要超时异步请求,请使用Abort方法


()

因此,如果状态未返回为Ok correct,我需要像处理异常一样处理此异常?是的,异常本质上是说服务器没有在给定的限制内响应。它没有响应的原因可能会有所不同。您可以尝试从引发的异常中获取http代码。看看这个答案: