The short answer to your question is "Your server is not returning all the data and you did ask the method to wait after all". But that is not helpful.
You might want to be a timeout on the HttpClient get async method. The code below is a contrived example of how to do so. Here is what the code is doing:
- A CancellationToken source is created that will signal the CancellationToken as cancelled after five hundred milliseconds.
- I get the default landing page for the Google search page. I'm using just a Geta sync instead of a GetstreamAsync because I don't can't think of a source of a stream response off the top of my head that I wouldn't have to code myself. It is the same principal though. On my network, the 500 milliseconds timeout is enough time, so I get a message of "Response code = OK".
- If I change the five hundred milliseconds to ten milliseconds the response does not complete before time runs out and I get a message of "A task was canceled."
So, when you say "await" it really does mean "wait here until the method I am calling completes unless I cancel the await earlier than that. Also remember there can be all kinds of CancellationTokenSources depending on what you need. This is just an example.
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace cancel
{
class Program
{
static async Task Main(string[] args)
{
CancellationTokenSource cancellationSource = new CancellationTokenSource(new TimeSpan(0,0,0,0,5000)); //Succeeds
//CancellationTokenSource cancellationSource = new CancellationTokenSource(new TimeSpan(0,0,0,0,10)); //Fails
CancellationToken cancellationToken = cancellationSource.Token;
var client = new HttpClient();
try
{
var result = await client.GetAsync("https://www.google.com",cancellationToken);
Console.WriteLine($"Response code = {result.StatusCode}");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}