【发布时间】:2019-11-20 14:37:26
【问题描述】:
我正在使用 C# 和 .NET Core 3.0
目前,当我向服务器发送 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();
我知道服务器在状态为 204 时也返回正文。我用 Curl 对其进行了测试,它返回此正文为 204 { "nextRequestInSec" : 10 }。
来自 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
对于其他响应(200、401、403、404、420、500),它可以正常工作,我也可以阅读正文。
有什么办法可以到达这个身体吗?
【问题讨论】:
-
看起来好像你无法得到它:github.com/dotnet/corefx/blob/…
-
您确定 curl 显示正文吗?我很怀疑,所以我自己做了一个测试,它切断了响应
-
@richzilla 我已经编辑了这个问题 - 看看那里的 curl 响应。
标签: c# asp.net-core dotnet-httpclient