request.GetResponse()超时

时间:2015-04-17 04:38:57

标签: httpresponse request-timed-out

Waker.cs

class Waker
{
    Timer timer;
    public Waker()
    {
        timer = null;
    }
    public void WakeUpApplicationPool(object obj)
    {
        string url = "http://www.example.com";
        try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Program.LogToFile("WakeUpApplicationPool: " + response.StatusDescription);
            response.Close();
        }
        catch (Exception ex)
        {
            Program.LogToFile("WakeUpApplicationPool_Error: " + ex.ToString());
        }
    }
    public void Start()
    {
        TimerCallback callback = new TimerCallback(WakeUpApplicationPool);
        int DUE_TIME = 0; //The amount of time to delay before the callback parameter invokes its methods.
        int PERIOD = int.Parse(ConfigurationManager.AppSettings["WakerIntervalPeriod"]); //The time interval (miliseconds) between invocations of the methods referenced by callback
        timer = new Timer(callback, null, DUE_TIME, PERIOD);
    }
    public void Stop()
    {
        timer.Dispose();
    }

}

的Program.cs:

static void Main(string[] args)
    {
        try
        {
            Waker waker = new Waker();
            waker.Start();
        }
        catch(Exception ex) 
        {
            LogToFile(ex.ToString());                
        }            
    }

日志文件:

15 Apr 2015 18:29:39 - WakeUpApplicationPool: OK

15 Apr 2015 18:31:39 - WakeUpApplicationPool: OK

15 Apr 2015 18:33:59 - WakeUpApplicationPool_Error: System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 205.144.171.35:80
   at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.GetResponse()
   at ConsoleReporting.Waker.WakeUpApplicationPool(Object obj)

15 Apr 2015 18:35:39 - WakeUpApplicationPool: OK

15 Apr 2015 18:37:39 - WakeUpApplicationPool: OK

15 Apr 2015 18:41:18 - WakeUpApplicationPool_Error: System.Net.WebException: The operation has timed out
   at System.Net.HttpWebRequest.GetResponse()
   at ConsoleReporting.Waker.WakeUpApplicationPool(Object obj)

15 Apr 2015 18:43:18 - WakeUpApplicationPool_Error: System.Net.WebException: The operation has timed out
   at System.Net.HttpWebRequest.GetResponse()
   at ConsoleReporting.Waker.WakeUpApplicationPool(Object obj)

15 Apr 2015 18:45:18 - WakeUpApplicationPool_Error: System.Net.WebException: The operation has timed out
   at System.Net.HttpWebRequest.GetResponse()
   at ConsoleReporting.Waker.WakeUpApplicationPool(Object obj)

15 Apr 2015 18:47:18 - WakeUpApplicationPool_Error: System.Net.WebException: The operation has timed out
   at System.Net.HttpWebRequest.GetResponse()
   at ConsoleReporting.Waker.WakeUpApplicationPool(Object obj)

问题是:

  1. 我的代码在达到超时错误后无效。但是在我重新启动Program.exe之后,它再次运行但是在10分钟后它出现了Timed Out错误。
  2. 我想使用此Program.exe唤醒托管服务提供商托管的应用程序池。
  3. 所以有人能说出理由和解决方案吗?我提到this,但它不适用于我的代码

1 个答案:

答案 0 :(得分:0)

WakerIntervalPeriod设置为10分钟而不是5分钟后,问题就解决了。