检查Web API是否可用的好/正确方法是什么? [英] what's a good/proper way to check whether a Web API is available?
问题描述
我有一个从API获取数据的应用程序:
I have an application which gets data from an API:
string jDoc = webclient.DownloadString(url);
但是,在进行此调用之前,我需要确保API可用.
Before I make this call, however, I need to make sure the API is available.
我该怎么做?
我应该只使用 TRY/CATCH
块还是有更好的方法?
Should I just use a TRY/CATCH
block or is there a better way of doing this?
推荐答案
Web API与其他任何网页一样,因此归结为如何查看网页是否响应?"
A Web API is like any other web page, so this comes down to "How do I see if a webpage is responding?"
根据文档,可以,只需使用尝试/捕获
.如果存在 WebException
,并且您确定地址有效,那么进行查询时就会出现问题.您可以检查WebException的 Response
和 Status
属性以了解更多信息.
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.
这篇关于检查Web API是否可用的好/正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!