using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
Console.Write("Enter target URL (must be your own server): ");
string url = Console.ReadLine();
Console.Write("Number of requests to send: ");
if (!int.TryParse(Console.ReadLine(), out int requestCount) || requestCount <= 0)
{
Console.WriteLine("Invalid number of requests.");
return;
}
using HttpClient client = new HttpClient();
int success = 0, fail = 0;
// Limit concurrency to avoid overwhelming your own system
SemaphoreSlim throttler = new SemaphoreSlim(10);
Task[] tasks = new Task[requestCount];
for (int i = 0; i < requestCount; i++)
{
// Stagger each request by 20ms
await Task.Delay(20);
await throttler.WaitAsync();
tasks[i] = Task.Run(async () =>
{
try
{
HttpResponseMessage response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
Interlocked.Increment(ref success);
else
Interlocked.Increment(ref fail);
}
catch
{
Interlocked.Increment(ref fail);
}
finally
{
throttler.Release();
}
});
}
await Task.WhenAll(tasks);
Console.WriteLine($"Completed. Success: {success}, Failed: {fail}");
}
}