2012-01-06 6 views
0

나는 간단한 콘솔 응용 프로그램을 작성하여 내 사이트가 항상 작동하는지 확인하여 확인합니다. 문제는 그 전날 내려 가서이었고, 그것은 나를 통지 didnt는, 그것은 오류가 있었다 :콘솔 응용 프로그램의 System.Net.WebException

system.net.webexception the operation has timed out at system.net.httpwebrequest.getresponse

을 나는 15000 밀리 초 그래서 15초으로 제한 시간을 설정합니다. 나는 요청할 때 방화범을보고 요청이 중단되었다고 말했고 상태 코드를 보지 못했습니다. 하지만 타임 아웃을 설정할 때 왜 내가이 expection을 얻는 지 궁금합니다.

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("ab[email protected]")); 
        contactMsg.From = new MailAddress("[email protected]"); 
        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(); 
      } 
     } 
    } 
} 

답변

1

사이트가 15 초 내에 응답하지 않으므로 WebException 예외가 발생합니다. Timeout은 예외를 throw하도록 설계되었습니다.

The Timeout property indicates the length of time, in milliseconds, until the request times out and throws a WebException. The Timeout property affects only synchronous requests made with the GetResponse method. To time out asynchronous requests, use the Abort method.

(MSDN)

+0

그래서 나는 상태 밤은이 같은 확인 올바른 반환 한 경우와 같은이 예외를 처리 할 필요가? – ios85

+0

예, 기본적으로 서버가 주어진 한도 내에서 응답하지 않는다는 것을 제외하고는 예외입니다. 응답하지 않는 이유는 다양 할 수 있습니다. 시도한 예외에서 HTTP 코드를 가져올 수 있습니다. 이 답변보기 : http://stackoverflow.com/questions/3614034/system-net-webexception-http-code – keyboardP

관련 문제