The GetAsync (uri) method is asynchronous. The result of its execution is the HttpResponseMessage object, which has the Content property. Content includes the asynchronous ReadAsStringAsync method.

This suggests that we get an HttpResponseMessage in which the body is not loaded yet, and the headers are already loaded. Ie we can check the size of the body and if it is not necessary, then do not ship. But I see another picture: getting the HttpResponseMessage results in a full body load. Is this true or am I wrong? How to still prevent unnecessary consumption of traffic?

Example:

public static void Test() { HttpClient client = new HttpClient(); //Ссылка на тяжелый файл var uri = new Uri("http://video.seclub.org/dl2.php?f=18848"); client.GetAsync(uri).ContinueWith((responce) => { //Вопреки ожиданиям сюда не попадаем до тех пор, пока полностью не будет загружно тело ответа Stopwatch watch = new Stopwatch(); watch.Start(); var bodyDownloading = responce.Result.Content.ReadAsByteArrayAsync(); byte[] body = bodyDownloading.Result; watch.Stop(); Console.WriteLine($"Загрузили {body.Length / 1024 / 1024} mb за {watch.ElapsedMilliseconds / 1000} секунд? Не думаю!"); }).Wait(); } 
  • Does not lead to full load of course. otherwise ReadAs ... Async would not be asynchronous - vitidev
  • And here leads. Look at my edit - I added an example. - Pavel
  • How do you determine at what point the response body was fully loaded? - Pavel Mayorov
  • one
    So the second parameter GetAsync needs to be put in HttpCompletionOption.ResponseHeadersRead - vitidev
  • Very, very good point. Thank you) - Pavel

0