I have a method which receives multiple url strings and is supposed to create HTTP requests for each of them, simultaneously, wait for them to finish and then return a list containing the result of each request.
I tried to implement the async/await
logic as illustrated with the code below:
private static readonly HttpClient httpClient = new HttpClient();
public class Response
{
public bool isError;
public string content;
}
public async void TestManyGetRequestsAsync(List<string> urls)
{
List<Response> responses = await GetManyRequestsAsync(urls);
foreach (Response response in responses)
{
Debug.LogFormat("Content: {0}", response == null ? "NULL" : response.content);
}
}
async Task<List<Response>> GetManyRequestsAsync(List<string> urls)
{
List<Task<Response>> tasks = new List<Task<Response>>();
foreach (string url in urls)
{
Task<Response> task = Task.Run<Response>(() => AsyncGetRequest(url)); // This line appears to do the same thing as the line below
// Task<RestAPI.Response> task = RestAPI.Get(url);
tasks.Add(task);
}
Response[] responses = await Task.WhenAll(tasks);
return responses.ToList();
}
public async Task<Response> AsyncGetRequest(string url)
{
Task<HttpResponseMessage> task = httpClient.GetAsync(url);
HttpResponseMessage message;
Debug.LogFormat("Sending GET request for {0}", url);
try
{
message = await task;
}
catch
{
return null;
}
Response response = new Response {
isError = !message.IsSuccessStatusCode,
content = await message.Content.ReadAsStringAsync()
};
Debug.LogFormat("Received GET response for {0}: {1}", url, response);
return response;
}
The issue is that every task is running sequentially, meaning that each task is on hold while the previous is in progress.
I have the impression that different factors may be involved (the way I implemented the async/await logic, the way requests are run, etc.) but I am not able to figure out a clear solution to my issue.
Any hint or suggestion would be highly appreciated, thanks!