【发布时间】:2017-10-09 16:23:29
【问题描述】:
我正在使用以下代码来完成外部 API 调用。
WebResponse response = request.GetResponse();
string JSONResult = null;
var data = response.GetResponseStream();
using (var reader = new StreamReader(data))
{
JSONResult = reader.ReadToEnd();
}
当外部 API 出现异常时,request.GetResponse 会抛出错误。但是,我无法获得显示的消息,例如
{
"Message": "No HTTP resource was found that matches the request URI '<site>/Foo'.",
"MessageDetail": "No type was found that matches the controller named 'Foo'."
}
虽然这显示在 Fiddler 和 Postman 中,但当它作为异常抛出时,我无法在任何地方收到此消息。
当外部 API 调用出错时,我如何获取此特定详细信息?
【问题讨论】:
-
try { } catch (Exception ex) { throw ex }
-
@AGrammerPro 不幸的是,它需要比这更多的工作。看我的回答。
-
@CodingYoshi 我赞成你的回答:)
标签: c# .net api error-handling