简体   繁体   English

当响应为 HttpStatusCode.NoContent 时,有什么方法可以获取 HttpResponseMessage.Content 正文?

[英]Is there any way how to get HttpResponseMessage.Content body when response is HttpStatusCode.NoContent?

I am using C# and .NET Core 3.0我正在使用 C# 和 .NET Core 3.0

Currently when I send GET request to a server and it returns response with status 204 , then it seems that System.Net.Http.HttpClient strips body from it and I cannot read its content.目前,当我向服务器发送 GET 请求并返回状态为204的响应时,似乎System.Net.Http.HttpClient从中剥离了正文,我无法读取其内容。

var request = new HttpRequestMessage(HttpMethod.Get,$"/gettask.ashx?testerror=20");
var response = await _httpClient.SendAsync(request);
var responseContent = await response.Content.ReadAsStringAsync();

I know the server is returning also body when status is 204. I have tested it with Curl and it returns this body for 204 { "nextRequestInSec": 10 } .我知道服务器在状态为 204 时也返回正文。我已经用 Curl 对其进行了测试,它返回此正文为 204 { "nextRequestInSec": 10 }

Verbose response from curl:来自 curl 的详细响应:

curl http://address.dom/testerror=204 -v
*   Trying xxx.xxx.xxx.xxx...
* TCP_NODELAY set
* Connected to address.dom (xxx.xxx.xxx.xxx) port 80 (#0)
> GET testerror=204 HTTP/1.1
> Host: address.dom
> User-Agent: curl/7.55.1
> Accept: */*
>
< HTTP/1.1 204 No Content
< Cache-Control: private
< Transfer-Encoding: chunked
< Content-Type: text/html; charset=utf-8
< Server: Microsoft-IIS/8.5
< X-AspNet-Version: 4.0.30319
< X-Powered-By: ASP.NET
< Date: Wed, 20 Nov 2019 13:45:00 GMT
<
{ "nextRequestInSec" : 10 }* Connection #0 to host address.dom left intact

For other responses (200, 401, 403, 404, 420, 500) it works correctly and I can read body as well.对于其他响应(200、401、403、404、420、500),它可以正常工作,我也可以阅读正文。

Is there any way how to get to this body?有什么方法可以到达这个身体吗?

As @peinarydevelopment mentions in their comment, the httpclient instance will not return a body for status codes that dont support one.正如@peinarydevelopment 在他们的评论中提到的那样, httpclient实例不会为不支持的状态代码返回正文。 The best solution to your problem is to fix the server (or ask whoever is responsible for it).解决您的问题的最佳方法是修复服务器(或询问负责它的人)。

If you really must deal with the broken HTTP server, you could drop down to TCP level and retrieve the packets directly from the socket, put that seems like a very long way around the problem.如果你真的必须处理损坏的 HTTP 服务器,你可以降到 TCP 级别并直接从套接字检索数据包,这似乎是一个很长的问题解决方法。

204 means there is no content, so before reading the content check the status code. 204 表示没有内容,所以在阅读内容之前检查状态码。 If you want to return data, change your status code because 204 should not.如果您想返回数据,请更改您的状态码,因为 204 不应该。

var request = new HttpRequestMessage(HttpMethod.Get,$"/gettask.ashx?testerror=20");
var response = await _httpClient.SendAsync(request);
var responseContent = (response.IsSuccessStatusCode && response.StatusCode !=204)? await 
                      response.Content.ReadAsStringAsync():null;

Well it appears that I was a bit blinded.好吧,看来我有点蒙蔽了。 The truth is that in .NET Core this is not possible according to the HttpClient事实是,根据HttpClient ,在 .NET Core 中这是不可能

Also if You look at it as the HTTP communication, it should follow HTTP specification , which says, that:此外,如果您将其视为 HTTP 通信,则它应遵循HTTP 规范,即:

The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields. 204 响应不能包含消息体,因此总是由 header 字段之后的第一个空行终止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HttpResponseMessage.Content为null - HttpResponseMessage.Content is null FileContentResult写入HttpResponseMessage.Content - FileContentResult write to HttpResponseMessage.Content HttpResponseMessage.Content 可以是 null 吗? - Can HttpResponseMessage.Content be null? 作为HttpResponseMessage.Content发送后,蒸汽何时关闭? - When does a steam close after sending it as HttpResponseMessage.Content? 从HttpResponseMessage.Content读取流式内容 - Reading streamed content from HttpResponseMessage.Content 读取 HttpResponseMessage.Content 在读取 webapi 2 令牌时抛出 Newtonsoft.Json.JsonReaderException - Reading HttpResponseMessage.Content throws Newtonsoft.Json.JsonReaderException when reading webapi 2 token 如何导入System.Net.Http.ObjectContent <object> 所以我可以将HttpResponseMessage.Content强制转换为它吗? - How to import System.Net.Http.ObjectContent<object> so I can cast HttpResponseMessage.Content to it? .NET 核心 3.1 中带有 HttpResponseMessage.Content 的意外 null 行为 - Unexpected null behavior with HttpResponseMessage.Content in .NET core 3.1 如何在PostAsJsonAsync之后捕获HttpResponseMessage.Content作为记录? - How can I capture the HttpResponseMessage.Content after a PostAsJsonAsync for our records? 从Flurl HttpResponseMessage获取回复正文 - Get response body from Flurl HttpResponseMessage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM