【实用】C#测试web服务是否可用

640?wx_fmt=jpeg


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("不可用");

            }



posted @ 2018-06-15 07:01  天使不哭  阅读(204)  评论(0编辑  收藏  举报