1

我只是想有一个基本SendAsync的方法将它的进度写入控制台,即使在 GET 的一堆 SO 答案之后,我也没有真正看到这对 POST 是如何工作的。

我正在测试的文件约为 100Mb,当它遇到SendAsync方法时,它会一直等待完成。

我可能会错过什么?

var request = new HttpRequestMessage
{
    RequestUri = endpoint,
    Method = httpMethod,
    Content = data
};

var result = string.Empty;
using (var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead))
using (var responseStream = await response.Content.ReadAsStreamAsync())
{
    int read;
    var offset = 0;
    var responseBuffer = new byte[1024];
    do
    {
        read = await responseStream.ReadAsync(responseBuffer, 0, responseBuffer.Length);
        result += Encoding.UTF8.GetString(responseBuffer, 0, read);
        offset += read;
        Console.WriteLine($"Offset: {offset}");
    } while (read != 0);
    response.EnsureSuccessStatusCode();
}
return JsonConvert.DeserializeObject<T>(result);
4

0 回答 0