如何检索基于json的错误而不是标准.net

时间:2016-10-22 12:36:00

标签: c# json webclient

我正在点击一个以json的形式返回错误的Web服务。当我使用Web客户端时,如何解释这些错误?我希望能够将这些传递给我的用户,这样他们就不会有错误。

  

{"成功":false,"错误":[
      {
        " propertyName":"参考",
        "原因":"'参考'不能为空。"
      },
      {
         " propertyName":"参考",
         "原因":"'参考'不应该是空的。"
      },       {           " propertyName":" Key",
          "原因":"' Key'必须介于10到15个字符之间。您输入了3个字符。"
      },       {           " propertyName":" Key",
          "原因":" Key不以零开头:123。"       }],"警告":[],"信息":[]

这就是我呼唤第三方api的方式,但是当我点击这一行时,我得到一个标准的.net错误抛回我的问题是我怎样才能获得上面json的值返回< / p>

var httpResponse =(HttpWebResponse)httpWebRequest.GetResponse();

我无法上传的屏幕截图

https://snag.gy/Itoy02.jpg

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  我已经尝试了以下内容,它让我有点进一步,但没有在错误中的两个列表属性的desps

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;

        }

所以我创建了这个类,但我不确定如何将错误去除错误

 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

我在这里尝试了下面的内容,但是当我在第一篇文章中没有填充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();

        }

1 个答案:

答案 0 :(得分:0)

您可以阅读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
    }
}