简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM