在C#中,用HttpClient调用Web API并且通过Content.ReadAsStringAsync()读取响应内容时,如果出现500错误(InternalServerError),会得到一个包含错误信息的json字符串:

{
"Message":"An error has occurred.",
"ExceptionMessage":"",
"ExceptionType":"",
"StackTrace":"",
"InnerException":
{
"Message":"",
"ExceptionMessage":"",
"ExceptionType":",
"StackTrace":"",
"InnerException":
{
"Message":"",
"ExceptionMessage":"",
"ExceptionType":"",
"StackTrace":""
}
}
}

这样一个复杂的字符串可读性很差,通常只需要部分信息(比如ExceptionMessage)就可以知道错误的情况。

那如何读取所需的部分信息呢?

开始用的是 Microsoft.AspNet.WebApi.Client + dynamic,实现代码如下:

var response = await _httClient.GetAsync(url);
dynamic content = await response.Content.ReadAsAsync<ExpandoObject>();
Console.WriteLine(content.ExceptionMessage);

后来一想,这个json字符串也是某种类型的实例序列化出来的,找到这个类型,然后直接反序列化这个类型的实例,岂不更简单。

找了找,发现原来就是Microsoft.AspNet.WebApi.Core中的HttpError:

namespace System.Web.Http
{
[XmlRoot("Error")]
public sealed class HttpError : Dictionary<string, object>, IXmlSerializable
{
public HttpError();
public HttpError(string message);
public HttpError(ModelStateDictionary modelState, bool includeErrorDetail);
public HttpError(Exception exception, bool includeErrorDetail);
public string ExceptionMessage { get; set; }
public string ExceptionType { get; set; }
public HttpError InnerException { get; }
public string Message { get; set; }
public string MessageDetail { get; set; }
public HttpError ModelState { get; }
public string StackTrace { get; set; }
public TValue GetPropertyValue<TValue>(string key);
}
}

于是改用下面更简单的实现代码:

var response = await _httClient.GetAsync(url);
Console.WriteLine((await response.Content.ReadAsAsync<HttpError>()).ExceptionMessage);

注:需要nuget安装Microsoft.AspNet.WebApi.Client与Microsoft.AspNet.WebApi.Core

HttpClient读取ASP.NET Web API错误信息的简单方法的更多相关文章

  1. 使用HttpClient消费ASP.NET Web API服务

    本篇体验使用HttpClient消费ASP.NET Web API服务,例子比较简单. 依次点击"文件","新建","项目". 选择&quo ...

  2. 使用HttpClient对ASP.NET Web API服务实现增删改查

    本篇体验使用HttpClient对ASP.NET Web API服务实现增删改查. 创建ASP.NET Web API项目 新建项目,选择"ASP.NET MVC 4 Web应用程序&quo ...

  3. 通过HttpClient 调用ASP.NET Web API

    在前面两篇文章中我们介绍了ASP.NET Web API的基本知识和原理,并且通过简单的实例了解了它的基本(CRUD)操作.我们是通过JQuery和Ajax对Web API进行数据操作.这一篇我们来介 ...

  4. 使用HttpClient操作ASP.NET Web API 2.1增删改查

    使用NuGet包安装Microsoft ASP.NET Web API 2.1 Client Libraries, 调用方式代码如下: HttpClient client = new HttpClie ...

  5. 学习ASP.NET Web API框架揭秘之“HTTP方法重写”

    最近在看老A的<ASP.NET Web API 框架揭秘>,这本书对于本人现阶段来说还是比较合适的(对于调用已经较为熟悉,用其开发过项目,但未深入理解过很多内容为何可以这样“调用”).看到 ...

  6. 支持Ajax跨域访问ASP.NET Web Api 2(Cors)的简单示例教程演示

    随着深入使用ASP.NET Web Api,我们可能会在项目中考虑将前端的业务分得更细.比如前端项目使用Angularjs的框架来做UI,而数据则由另一个Web Api 的网站项目来支撑.注意,这里是 ...

  7. 让ASP.NET Web API支持$format参数的方法

    在不使用OData的情况下,也可以让ASP.NET Web API支持$format参数,只要在WebApiConfig里添加如下三行红色粗体代码即可: using System; using Sys ...

  8. [转]让ASP.NET Web API支持$format参数的方法

    本文转自:http://www.cnblogs.com/liuzhendong/p/4228592.html 在不使用OData的情况下,也可以让ASP.NET Web API支持$format参数, ...

  9. 在一个空ASP.NET Web项目上创建一个ASP.NET Web API 2.0应用

    由于ASP.NET Web API具有与ASP.NET MVC类似的编程方式,再加上目前市面上专门介绍ASP.NET Web API 的书籍少之又少(我们看到的相关内容往往是某本介绍ASP.NET M ...

随机推荐

  1. gsons

    java 处理 json格式字符串,目前只使用过Google的Gson库. pom: <dependency> <groupId>com.google.code.gson< ...

  2. 用tcc遇到的一个大坑

    在centos6.5 x86_64服务器上编译安装完tcc, 版本0.9.25(在github上clone的),似乎一切正常 但当用tcc来编译"hello, world"程序时, ...

  3. phpunit.xml

    <phpunit bootstrap="vendor/autoload.php"> <testsuites> <testsuite name=&quo ...

  4. switch(面试)

    public static void Output() { ; ) { i--; switch (i) { : Console.WriteLine(i.ToString()); break; : Co ...

  5. Django concept

    1. MVC in Django http://stackoverflow.com/questions/6621653/django-vs-model-view-controller https:// ...

  6. java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView

    最近在学习drawerLayout时,遇到这个bug.如下示: java.lang.ClassCastException: android.widget.RelativeLayout cannot b ...

  7. detection reading

    1512.07729v1 G-CNN an Iterative Grid Based Object Detector,先基于空间金字塔生成很多矩形框,然后把这些矩形框作为regions,进行fast ...

  8. Java核心知识点学习----线程同步工具类,CyclicBarrier学习

    线程同步工具类,CyclicBarrier日常开发较少涉及,这里只举一个例子,以做备注.N个人一块出去玩,相约去两个地方,CyclicBarrier的主要作用是等待所有人都汇合了,才往下一站出发. 1 ...

  9. PLSQL数据导入导出问题解决(空表、大字段表、表空间错误等)

    PLSQL使用方法简单,平常使用较多,但在平常使用过程中,遇到一些问题,下面简单罗列并进行解决.这些解决方法大多通过网络查找获得,这里只是进行简单整理. 使用的数据库版本为:Oracle11g. 通用 ...

  10. mysql的一个特殊问题 you can't specify target table 'cpn_regist' for update in FROM clause

    今天在操作数据库的时候遇到了一个问题,sql语句如下: UPDATE cpn_yogurt_registration SET dep1Name = '1' WHERE `key` in  (SELEC ...