C # test whether Web service is available

Source: Internet
Author: User

Reprint: http://www.cnblogs.com/xienb/p/3443282.html

WinForm clients often need to call WebService or WCF for data interaction, but the remote service may not exist or the server is unavailable, and the client can only be unavailable by timing out or capturing the exception-aware service. There is actually a COM excuse for using fast detection to detect the state of the service that will be invoked. Right- reference COM component : Microsoft xml,v3.0, the code is as follows:

stringURL ="http://127.0.0.1/MySrv/MsgService.svc"; XMLHTTP http=NewXMLHTTP (); Try{Http.open ("GET"Urlfalse,NULL,NULL);                Http.send (URL); intStatus =Http.status; if(Status = = $) {Console.WriteLine ("Success"); }                Else{Console.WriteLine ("Unavailable Status:"+status.                ToString ()); }            }            Catch{Console.WriteLine ("Not available"); }

Method Two:

 /// <summary>        ///Check if WebService is available/// </summary>        /// <param name= "url" ></param>        /// <returns></returns>        Private Static BOOLCheckactivewebservice (stringURL) {            Try            {                stringURI = URL +"? wsdl"; HttpWebRequest Request=(HttpWebRequest) webrequest.create (URI); Request. useDefaultCredentials=true; Request. Method="GET"; Request. Timeout= the; HttpWebResponse Response=(HttpWebResponse) request.                GetResponse (); if(Response. StatusCode = = Httpstatuscode.ok && response. Contenttype.substring (0,8) =="Text/xml")                {                    return true; }                Else                {                    return false; }            }            Catch(WebException e) {System.Diagnostics.Trace.Write (e.message); return false; }        }
View Code

C # test whether Web service is available

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.