是否需要在 ReadAsStringAsync 之后检查 IsSuccessStatusCode?

Is there a need to check `IsSuccessStatusCode` after `ReadAsStringAsync`?

考虑:

HttpClient client = new HttpClient();
var response = await client.GetAsync("url");
if (response.IsSuccessStatusCode) return false;
var getResponsestring = await response.Content.ReadAsStringAsync();
// check response.IsSuccessStatusCode again ??

我现在需要再次检查 IsSuccessStatusCode 吗?还是异常处理涵盖所有错误?

也就是说 IsSuccessStatusCode 可以是 GetAsync 之后的 trueReadAsStringAsync 之后的 false 吗?

您不必在调用ReadAsStringAsync后检查IsSuccessStatusCode 属性,在调用client.GetAsync后检查一次就足够了。 ReadAsStringAsync 不会改变 IsSuccessStatusCode 的值。

作为 docs 状态:

Gets a value that indicates whether the HTTP response was successful.