Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's a good/proper way to check whether a Web API is available?

I have an application which gets data from an API:

string jDoc = webclient.DownloadString(url);

Before I make this call, however, I need to make sure the API is available.

How would I go about doing this?

Should I just use a TRY/CATCH block or is there a better way of doing this?

like image 312
JJ. Avatar asked Nov 13 '22 01:11

JJ.


1 Answers

A Web API is like any other web page, so this comes down to "How do I see if a webpage is responding?"

As per the documentation, sure, just use try/catch. If there's a WebException, and you're sure the address is valid, then there was some issue in making your query. You can check the Response and Status properties of the WebException to learn more.

like image 164
Scott Mermelstein Avatar answered Nov 15 '22 00:11

Scott Mermelstein