gpt4 book ai didi

c# - 状态码不成功时无法读取HttpResponseMessage内容

转载 作者:行者123 更新时间:2023-12-02 11:03:13 25 4
gpt4 key购买 nike

我有一项服务正在使用 HttpClient 使用 SMS REST API:

HttpClient http = this._httpClientFactory.CreateClient();
// Skipped: setup HttpRequestMessage
using (HttpResponseMessage response = await http.SendAsync(request))
{
try
{
_ = response.EnsureSuccessStatusCode();
}
catch (HttpRequestException)
{
string responseString = await response.Content.ReadAsStringAsync(); // Fails with ObjectDisposedException
this._logger.LogInformation(
"Received invalid HTTP response status '{0}' from SMS API. Response content was {1}.",
(int)response.StatusCode,
responseString
);
throw;
}
}

API 返回错误,但我希望能够记录它。因此,我需要记录失败的状态代码(我可以从 response.StatusCode 中读取)和关联的内容(可能包含其他错误有用的详细信息)。

此代码在指令 await response.Content.ReadAsStringAsync() 上失败,但出现以下异常:

System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.Net.Http.HttpConnection+HttpConnectionResponseContent'.
    Module "System.Net.Http.HttpContent", in CheckDisposed
    Module "System.Net.Http.HttpContent", in ReadAsStringAsync

一些消息来源建议,当状态代码不在成功范围 (200-299) 内时,您不应该读取响应内容,但是如果响应确实包含有用的错误详细信息怎么办?

使用的.NET版本:AWS lambda linux运行时上的.NET Core 2.1.12。

最佳答案

好的,显然这是 a known issue在 .NET API 中,该问题已在 .NET Core 3.0 中得到解决。 response.EnsureSuccessStatusCode() 实际上是在处理响应内容。它的实现方式据说是为了帮助用户:

// Disposing the content should help users: If users call EnsureSuccessStatusCode(), an exception is
// thrown if the response status code is != 2xx. I.e. the behavior is similar to a failed request (e.g.
// connection failure). Users don't expect to dispose the content in this case: If an exception is
// thrown, the object is responsible fore cleaning up its state.

这是一种不良行为,已从 3.0 中删除。与此同时,我只是在日志之前切换到使用 IsSuccessStatusCode:

HttpClient http = this._httpClientFactory.CreateClient();
// Skipped: setup HttpRequestMessage
using (HttpResponseMessage response = await http.SendAsync(request))
{
if (!response.IsSuccessStatusCode)
{
string responseString = await response.Content.ReadAsStringAsync(); // Fails with ObjectDisposedException
this._logger.LogInformation(
"Received invalid HTTP response status '{0}' from SMS API. Response content was {1}.",
(int)response.StatusCode,
responseString
);
_ = response.EnsureSuccessStatusCode();
}
}

有点多余,但应该可以。

关于c# - 状态码不成功时无法读取HttpResponseMessage内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59679127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com