Multiple HttpClient Get Requests


Below is a method that makes concurrent HTTP GET requests to a specified URL, using HttpClient, TransformBlock, and ActionBlock, which are classes provided by .NET for concurrent programming.

 

/// <summary>
/// This will print you the status code.
/// </summary>
/// <param name="amountOfRequests"></param>
public static async Task MultipleGetRequests(int amountOfRequests)
{
var httpClient = new HttpClient();
const string urlToTest = "https://example.com";

var transformBlock = new TransformBlock<int, HttpResponseMessage>(async _ =>
{
var response = await httpClient.GetAsync(urlToTest);
return response;
});

var actionBlock = new ActionBlock<HttpResponseMessage>(response =>
{
Console.WriteLine(response.StatusCode);
});

// Link the blocks
transformBlock.LinkTo(actionBlock, new DataflowLinkOptions { PropagateCompletion = true });

for (var i = 0; i < amountOfRequests; i++)
{
transformBlock.Post(i);
}

transformBlock.Complete();
await actionBlock.Completion;
}

 

Explanation

var transformBlock = new TransformBlock<int, HttpResponseMessage>(async _ =>
{
    var response = await httpClient.GetAsync(urlToTest);
    return response;
});

A TransformBlock is declared here. It is part of the TPL Dataflow library provided in .NET, which provides a foundation for message passing and parallel programming. The TransformBlock processes an input (an integer which is ignored here), makes an HTTP GET request to the URL and outputs the HttpResponseMessage.

 

var actionBlock = new ActionBlock<HttpResponseMessage>(response =>
{
    Console.WriteLine(response.StatusCode);
});

An ActionBlock is declared here. The ActionBlock is used to take the HttpResponseMessage from the TransformBlock and perform an action on it. In this case, it is simply printing the status code of the response to the console.

transformBlock.LinkTo(actionBlock, new DataflowLinkOptions { PropagateCompletion = true });

Here, the TransformBlock and ActionBlock are linked so that when an item is processed by the TransformBlock, it is passed to the ActionBlock. The PropagateCompletion option ensures that when the TransformBlock completes, completion is propagated to the ActionBlock.

 

for (var i = 0; i < amountOfRequests; i++)
{
    transformBlock.Post(i);
}

A loop is used to post items to the TransformBlock which will trigger the processing.

 

transformBlock.Complete();
await actionBlock.Completion;

Finally, Complete is called on TransformBlock to signal that no more items will be posted, and await is used on the ActionBlock.Completion property to asynchronously wait until all processing has completed before returning from the method.

 


No files yet, migration hasn't completed yet!