# Awaiting multiple HTTP Requests

Scenario : Sending a POST to 2 different endpoints (not related to or dependent on each other).

I'm really just not sure if I went about this correctly, and whether or not the responses will have returned from the context before the method returns. I have read some about async/await and the context for which it operates, etc., but I'm still not sure.

class Program
{
static void Main(string[] args)
{
{
})
.GetAwaiter()
.GetResult();

}

{
List<HttpResponseMessage> responseMessages = new List<HttpResponseMessage>();

HttpClientHandler handler = new HttpClientHandler();

using (var httpClient = new HttpClient(handler))
{
var response1 = await httpClient.SendAsync(request1);
var response2 = await httpClient.SendAsync(request2);

responseMessages.AddRange(new [] { response1 , response2 });

return responseMessages;
}
}

}


I'm wondering if :

1. Task.Run( async () => ....) is correct here, as well as GetAwaiter/GetResult
2. Can the SendRequests method ever return before the requests finish / get resolved?

Task.Run(async () =>
{
})


is a very long-winded way of writing

SendRequests(cookie, request1, request2)


.GetAwaiter().GetResult() is also a long-winded way of writing .Result, but since you're ignoring the return value it's probably necessary.

Can the SendRequests method ever return before the requests finish / get resolved?

Of course. Otherwise it would return List<HttpResponseMessage> rather than Task<List<HttpResponseMessage>>. However, assuming that wasn't the question you actually intended to ask...

The doc for SendAsync says

The returned task object will complete once the entire response including content is read.

Since you await both of the tasks, the responses must finish before the SendRequests task will finish. (Of course, if the first one throws an exception then the second won't even be sent).

It's not clear to me whether the intention was for the requests to be made in parallel. That seems to be implied by

to 2 different endpoints (not related to or dependent on each other).

In that case you would want to create the tasks and then wait for them both:

var responseTask1 = httpClient.SendAsync(request1);

Although I have to say that I don't understand the purpose of creating responseMessages in the outer scope.
• Thanks for reviewing it and for the information. I think I leaned toward trying to use async / await because of the SendAsync method, but what you're saying is that there's really no benefit with async here at all? – Mark Apr 19 '17 at 14:48
• Be advised, that .GetAwaiter().GetResult() encapsulates the occurring Exception in an AggregateException, while Task.Run() doesn't. :-) – bl4y. Apr 24 '17 at 8:20