C#: How to programmatically check a web service is up and running?

后端 未结 2 1994
不知归路
不知归路 2020-12-09 04:04

I need to create an C# application that will monitor whether a set of web services are up and running. User will select a service name from a dropdown. The program need to

2条回答
  •  -上瘾入骨i
    2020-12-09 04:31

    I may be too late with the answer but this approach works for me. I used Socket to check if the process can connect. HttpWebRequest works if you try to check the connection 1-3 times but if you have a process which will run 24hours and from time to time needs to check the webserver availability that will not work anymore because will throw TimeOut Exception.

    Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    var result = socket.BeginConnect("xxx.com", 80, null, null);
                    bool success = result.AsyncWaitHandle.WaitOne(3000,false); // test the connection for 3 seconds
                    var resturnVal = socket.Connected;
                    if (socket.Connected)
                        socket.Disconnect(true);
                    socket.Dispose();
                    return resturnVal;
    

提交回复
热议问题