嗨,大家好我正在尝试一个头部请求来获取文件的内容长度(它的大> = 200MB)而且简单只是抛出一个异常,说“抛出类型'System.OutOfMemoryException'的异常” “。因为这是HEAD方法,这应该不是问题吧?
这是我的代码:
using (HttpClient client = new HttpClient())
{
using (var request = new HttpRequestMessage()
{
RequestUri = new Uri(streamLink.StreamLink),
Method = HttpMethod.Head
})
{
using (var response = await client.SendAsync(request))
{
var restatus = response.StatusCode == HttpStatusCode.OK;
if (restatus)
{
var filesize = ConvertBytesToMegabytes(response.Content.Headers.ContentLength);
CalculateStreamQuality(filesize, streamLink, runtime);
}
}
}
}
答案 0 :(得分:2)
您需要调用SendAsync并将completion选项参数设置为: HttpCompletionOption.ResponseHeadersRead - 这将使SendAsync方法无需尝试分配缓冲区来保存(不存在的数据)有效载荷)。
更新您的代码以使用以下内容:
using (var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead))
{
...
}