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