Вы можете сделать что-то подобное с HttpClient:
using var client = new HttpClient(); // init client
// set headers
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Basic", Convert.ToBase64String(
System.Text.Encoding.ASCII.GetBytes(
$"{username}:{password}")));
client.DefaultRequestHeaders.Add("aw-tenant-code", $"{key}");
// generate list of requests
var list = new List<Task<HttpResponseMessage>>();
for (int i = 0; i < 10; i++) // count of parallel requests or other GET requests
{
// add requests to the list of tasks
list.Add(client.GetAsync("https://www.google.com/"));
}
// all requests finished
await Task.WhenAll(list);
foreach (var task in list)
{
// get result per each request
var result = await task;
}
Или, если вы хотите использовать HttpRequestMessage
var list = new List<Task<HttpResponseMessage>>();
var httpRequestMessage = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri("some uri"),
Headers = {
// headers
}
};
for (int i = 0; i < 10; i++)
{
list.Add(client.SendAsync(httpRequestMessage));
}
await Task.WhenAll(list);
foreach (var task in list)
{
var result = await task;
if (result.StatusCode == HttpStatusCode.OK)
{
// do something
}
// to read the content as string
var stringResult = await result.Content.ReadAsStringAsync();
// to deserialize the content as some entity
var someEntity = await result.Content.ReadAsAsync<MyCustomObject>();
}
Примечание: ваш метод должен быть асинхронным