1

I wrote a C# program which post HTTP heartbeat request every 5 second,it runs well at the beginning,but after about 20-40 mins, it keeps throwing webException at the method HttpWebRequest.BeginGetRequestStream( ) ( or at HttpWebResponse.EndGetResponse() if I don't need to post data).

I met this problem in some specific computers, whose performance wasn't that good.

I've made several attempts,but still get the same result:

  1. change another server address
  2. use socket to post http request, and get socketException when new a socket(before socket.connect())
  3. asynchronous and synchronous http request
  4. use .net 4.0 (I use .net3.5 currently)
  5. almost the same code in winForm and WPF program
  6. make sure do nothing with proxy or firewall

here is the exception stacktrace:

   System.Net.WebException: the underlying connection was closed:unable to connect to the remote server. 
---> System.Net.Sockets.SocketException: an invalid argument was supplied.
at System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout)
at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback)
at System.Net.Connection.CompleteStartConnection(Boolean async, HttpWebRequest httpWebRequest)

here is the kernal part of my code:

    public MainWindow()
    {
        InitializeComponent();
        timer = new Timer(new TimerCallback(delegate (object obj)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "GET";
                request.KeepAlive = false;
                IAsyncResult result = request.BeginGetResponse(new AsyncCallback(ResponseCallback), request);
                ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), request, 15000, true);
            }
            catch(Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }), null, Timeout.Infinite, Timeout.Infinite);
    }

    private void TimeoutCallback(object state, bool timeout)
    {
        if (timeout)
        {
            HttpWebRequest request = state as HttpWebRequest;
            if (request != null)
            {
                request.Abort();
            }
        }
    }

    private void ResponseCallback(IAsyncResult result)
    {
        try
        {
            HttpWebRequest request = (HttpWebRequest)result.AsyncState;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);

            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            string responseString = reader.ReadToEnd();
            responseStream.Close();
            reader.Close();
            response.Close();

            Console.WriteLine(responseString);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

I don't think there is anything wrong with the code, maybe is the .net problem or network problem, I am very grateful if someone give me some advices.

McHo
  • 23
  • 5
  • What happens if you ignore it? Will the next request go through ok? – nvoigt Mar 01 '16 at 08:45
  • no, it keep throwing same thing,seem like the network thing in this program doesn't work anymore until I restart it. – McHo Mar 01 '16 at 08:51
  • @nvoigt I think maybe the sockets were used out, but since I've closed the response and stream.It is necessary to do something like `request = null;` or `System.GC.Collect();` when many requests were posted ? – McHo Mar 01 '16 at 09:17
  • @McHo you can try one of [these answers](http://stackoverflow.com/questions/1459475/system-net-webexception-the-underlying-connection-was-closed-the-connection-wa) – NoName Mar 01 '16 at 09:48
  • Do any of those classes you use have an `IDisposable` interface? Have you run static code analysis of Visual Studio? – nvoigt Mar 01 '16 at 09:50
  • @nvoigt Yes, I used `System.Threading.Timer`,will it be any threading problem? I'm gonna try `System.Timers.Timer`. – McHo Mar 02 '16 at 03:24
  • Did you find the solution to this? I'm having the same problem. – EJoshuaS - Stand with Ukraine Jan 24 '18 at 21:50
  • Have you guys found any answer ?? – Dot_NET Pro Apr 30 '18 at 10:22

0 Answers0