C#测试web服务是否可用

 winform客户端经常需要调用webservice或者WCF进行数据交互,但是远程服务有可能不存在或者服务器不可用,客户端只能通过超时或者捕获到异常感知服务不可用。其实有一个COM借口可用快速的检测检测将要调用服务的状态。引用COM组件:Microsoft XML,v3.0,代码如下:

  

复制代码

            string url = "http://127.0.0.1/MySrv/MsgService.svc";
            XMLHTTP http = new XMLHTTP();
            try
            {
                http.open("GET", url, false, null, null);
                http.send(url);
                int status = http.status;
                if (status == 200)
                {
                    Console.WriteLine("成功");
                }
                else
                {
                    Console.WriteLine("不可用status:"+status.ToString());
                }
            }
            catch
            {
                Console.WriteLine("不可用");
            }

复制代码

猜你喜欢

转载自blog.csdn.net/OnlyRu/article/details/81946365