我正在尝试将以下读取 HttpContent 的完整字符串响应的代码转换为字符串,以仅读取特定的最大字符数。现有代码:
private static async Task<string> GetContentStringAsync(HttpContent content)
{
string responseContent = await content.ReadAsStringAsync().ConfigureAwait(false);
return responseContent;
}
我现在拥有的代码:
private static async Task<string> GetContentStringAsync(HttpContent content, int ResponseContentMaxLength)
{
string responseContent;
Stream responseStream = await content.ReadAsStreamAsync().ConfigureAwait(false);
using (StreamReader streamReader = new StreamReader(responseStream))
{
// responseContent = Data from streamReader until ResponseContentMaxLength
}
return responseContent;
}
我是 StreamReader 和 HttpContent 操作的新手。有没有办法做到这一点?