3
我正在使用以下代碼來完成外部API調用。在Postman Body中捕獲外部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和郵差,我不能得到這個消息,當它被拋出作爲例外。
當外部API調用發生錯誤時,如何獲取此特定詳細信息?
try {} catch(Exception ex){throw ex} – AGrammerPro
@AGrammerPro不幸的是它需要更多的工作。看到我的答案。 – CodingYoshi
@CodingYoshi我upvoted你的答案:) – AGrammerPro