views:

315

answers:

4

How i can determine when a web service is available (on-line)? in Delphi or C#?

+4  A: 

Try to use it. If it works, then it's available. If it doesn't, then it isn't (or you have network-connection problems between you and the server). There's no magic here.

Rob Kennedy
+10  A: 

Just do a valid call to the web service. If it times out, or you get a 404 error, the web service is not available.

Robert Harvey
+2  A: 

You can try this method..

    public bool IsAddressAvailable(string address)
    {
        try
        {
            System.Net.WebClient client = new WebClient();
            client.DownloadData(address);
            return true;
        }
        catch
        {
            return false;
        }
    }

...and call it like this...

MessageBox.Show(IsAddressAvailable("http://localhost/my.webservices/mywebservice.asmx").ToString());
karbon
+1  A: 

Finally I Wrote this code in Delphi .Net

function WebService_OnLine(UrlWebService:String): Boolean;
Var
urlCheck  : uri;
request   : WebRequest;
response  : WebResponse;
begin
MemoLogWebService.Lines.Add('Testing WebService');
urlCheck    := Uri.Create(UrlWebService);
request     := WebRequest.CreateDefault(urlCheck);
request.Timeout := 20000;
    try
         response :=request.GetResponse();
         MemoLogWebService.Lines.Add(response.Headers.ToString);
         Result:=True;
    except
      on E : Exception do
      Begin
         MemoLogWebService.Lines.Add(E.Message);
         Result:=False;
      End;
    end;
End;

P.S : Thank you very much for giving me the inspiration.

RRUZ
You should narrow your exception trap to communication based exceptions. You rarely if ever want to trap Exception since that could be a catastrophic error in addition to communication based exceptions.
Jim McKeeth