1

I'm simply trying to have a basic SendAsync method write it's progress to the console, and even after a bunch of SO answers for GET, I'm not really seeing how this works for POST.

The file I'm testing with is ~ 100Mb and when it hits the SendAsync method, it sit until complete.

What might I be missing?

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);
Chase Florell
  • 46,378
  • 57
  • 186
  • 376
  • MSDN clearly says for SendAsync-`This operation will not block. The returned task object will complete once the entire response including content is read.` https://msdn.microsoft.com/en-us/library/hh138176(v=vs.118).aspx – Amit Apr 25 '17 at 07:59
  • sorry, it's not hanging, it's awaiting the `SendAsync` and thus the `Console.WriteLine` doesn't run until after the upload completes. – Chase Florell Apr 25 '17 at 16:06
  • That is what MSDN is also saying. Task object for sendAsync will be only complete when complete response is served. There is no intermittent things supported. Also, what does await really means? Isn't it wait till upload finishes ? – Amit Apr 26 '17 at 08:21
  • await doesn't `Wait()` it allows other processes to continue while the upload runs. [Here's an example](https://gist.github.com/ChaseFlorell/cbb2ad05228689e8cd1edb8f555b9e8b) of how you can run two processes at the same time. – Chase Florell Apr 26 '17 at 18:16
  • Please check once again.`The await operator is applied to a task in an asynchronous method to suspend the execution of the method until the awaited task completes. The task represents ongoing work.` https://msdn.microsoft.com/en-us/library/hh156528.aspx – Amit Apr 27 '17 at 05:43
  • the comments are derailed. I know exactly what await does. What I don't know is how to get POST progress off of `SendAsync`. My limited research shows it's not built in. – Chase Florell Apr 27 '17 at 17:33

0 Answers0