我正在尝试从400错误请求中捕获错误消息。在邮递员中,我收到400个错误的请求,而从端点获得的正文是:
{
"error": {
"message": "400 - Client no valid",
"type": "USER_ERROR"
},
"location": "..."
}
所以我试图在代码中捕获该错误,如下所示:
catch (WebException ex)
{
using (WebResponse response = ex.Response)
{
HttpWebResponse httpResponse = (HttpWebResponse)response;
Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
using (Stream data = response.GetResponseStream())
using (var reader = new StreamReader(data))
{
string text = reader.ReadToEnd();
OnError(AppResources.ErrorTitle, text);
}
}
}
但是“文本”始终为“”,如何从400错误的请求中获取错误正文?