C# HttpClient PostAsJsonAsync -> 读取字符串时出错

C# HttpClient PostAsJsonAsync -> Error reading string

您好,我尝试通过服务器端调用 Web api:

using (var client = new HttpClient())
        {
            using (var rsp = client.PostAsJsonAsync<Request>(url, model).Result)
            {
                if (!rsp.IsSuccessStatusCode)
                {
                    // throw an appropriate exception
                }
                var result = rsp.Content.ReadAsAsync<string>().Result;
            }
        }

但我收到错误

Error reading string. Unexpected token: StartObject. Path '', line 1, position 1.

如果我尝试从 jQuery

调用相同的 url
$.post('http://localhost/api/Test')

服务器return

 HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json
Expires: -1
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sun, 25 Oct 2015 12:15:56 GMT
Content-Length: 104

{
  "Header": {
    "Token": "Response",
    "Timestamp": "2015-10-25T14:15:56.0092197+02:00"
  }
}

"model" 到达 api 控制器,但我无法收到请求的响应。

ReadAsAsync<T> 尝试将响应反序列化为 T 类型。在这种情况下,您是说要将 JSON 反序列化为字符串,这实际上没有意义。使用与响应匹配的类型(即包含 HeaderToken 等的自定义数据结构),或者如果您真的想获取字符串,请使用 ReadAsStringAsync()