如何检索基于 json 的错误而不是标准 .net
How do I retrieve json based errors instead of standard .net
我正在访问一个 Web 服务,returns 它的错误是 json。使用 Web 客户端时如何解释这些错误?我希望能够将这些传递给我的用户,这样他们就不会遇到错误。
{ "success": false, "errors": [
{
"propertyName": "Reference",
"reason": "'Reference' must not be empty."
},
{
"propertyName": "Reference",
"reason": "'Reference' should not be empty."
},
{
"propertyName": "Key",
"reason": "'Key' must be between 10 and 15 characters. You entered 3 characters."
},
{
"propertyName": "Key",
"reason": "Key does not start with zero: 123."
} ], "warnings": [], "information": []
这就是我呼叫第三方的方式 api 但是当我点击这条线时,我收到了一个标准的 .net 错误,我的问题是我如何才能获取上面json 后面
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
无法上传的截图
public void TransferToSlate(string json, string url)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
编辑 2
我尝试了以下方法,它让我更进一步,但没有对错误中的两个列表属性感到失望
public void TransferToSlate(string json, string url)
{
try {
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
catch (WebException ex){
var resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
dynamic obj = JsonConvert.DeserializeObject(resp);
var messageFromServer = obj.error.message;
}
所以我创建了这个 class 但我不确定如何反序列化它的错误
public class ErrorMessage
{
public string PropertyName { get; set; }
public string Reason { get; set; }
public override string ToString()
{
return string.IsNullOrWhiteSpace(PropertyName)
? Reason
: string.Format("{0} : {1}", PropertyName, Reason);
}
}
编辑 3
我在这里尝试了以下操作,但当我不应该用我的第一个 post.
中的 json 的错误填充时,它只是返回 null
catch (WebException ex){
var resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
ErrorMessage error = JsonConvert.DeserializeObject<ErrorMessage>(resp);
var fieldinformation = error.PropertyName.ToString();
var messageFromServer = error.Reason.ToString();
}
您可以阅读来自 WebException
的回复:
try
{
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
catch(WebException ex)
{
var httpResponse = ex.Response as HttpWebResponse;
if (httpResponse != null)
{
// process the response
}
}
我正在访问一个 Web 服务,returns 它的错误是 json。使用 Web 客户端时如何解释这些错误?我希望能够将这些传递给我的用户,这样他们就不会遇到错误。
{ "success": false, "errors": [
{
"propertyName": "Reference",
"reason": "'Reference' must not be empty."
},
{
"propertyName": "Reference",
"reason": "'Reference' should not be empty."
}, { "propertyName": "Key",
"reason": "'Key' must be between 10 and 15 characters. You entered 3 characters."
}, { "propertyName": "Key",
"reason": "Key does not start with zero: 123." } ], "warnings": [], "information": []
这就是我呼叫第三方的方式 api 但是当我点击这条线时,我收到了一个标准的 .net 错误,我的问题是我如何才能获取上面json 后面
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
无法上传的截图
public void TransferToSlate(string json, string url)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
编辑 2 我尝试了以下方法,它让我更进一步,但没有对错误中的两个列表属性感到失望
public void TransferToSlate(string json, string url)
{
try {
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
catch (WebException ex){
var resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
dynamic obj = JsonConvert.DeserializeObject(resp);
var messageFromServer = obj.error.message;
}
所以我创建了这个 class 但我不确定如何反序列化它的错误
public class ErrorMessage
{
public string PropertyName { get; set; }
public string Reason { get; set; }
public override string ToString()
{
return string.IsNullOrWhiteSpace(PropertyName)
? Reason
: string.Format("{0} : {1}", PropertyName, Reason);
}
}
编辑 3
我在这里尝试了以下操作,但当我不应该用我的第一个 post.
中的 json 的错误填充时,它只是返回 null catch (WebException ex){
var resp = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
ErrorMessage error = JsonConvert.DeserializeObject<ErrorMessage>(resp);
var fieldinformation = error.PropertyName.ToString();
var messageFromServer = error.Reason.ToString();
}
您可以阅读来自 WebException
的回复:
try
{
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
catch(WebException ex)
{
var httpResponse = ex.Response as HttpWebResponse;
if (httpResponse != null)
{
// process the response
}
}