fork download
  1. using System;
  2. using System.Net.Http;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5.  
  6. class Program
  7. {
  8. static async Task Main(string[] args)
  9. {
  10. Console.Write("Enter target URL (must be your own server): ");
  11. string url = Console.ReadLine();
  12.  
  13. Console.Write("Number of requests to send: ");
  14. if (!int.TryParse(Console.ReadLine(), out int requestCount) || requestCount <= 0)
  15. {
  16. Console.WriteLine("Invalid number of requests.");
  17. return;
  18. }
  19.  
  20. using HttpClient client = new HttpClient();
  21. int success = 0, fail = 0;
  22.  
  23. // Limit concurrency to avoid overwhelming your own system
  24. SemaphoreSlim throttler = new SemaphoreSlim(10);
  25.  
  26. Task[] tasks = new Task[requestCount];
  27. for (int i = 0; i < requestCount; i++)
  28. {
  29. // Stagger each request by 20ms
  30. await Task.Delay(20);
  31.  
  32. await throttler.WaitAsync();
  33. tasks[i] = Task.Run(async () =>
  34. {
  35. try
  36. {
  37. HttpResponseMessage response = await client.GetAsync(url);
  38. if (response.IsSuccessStatusCode)
  39. Interlocked.Increment(ref success);
  40. else
  41. Interlocked.Increment(ref fail);
  42. }
  43. catch
  44. {
  45. Interlocked.Increment(ref fail);
  46. }
  47. finally
  48. {
  49. throttler.Release();
  50. }
  51. });
  52. }
  53.  
  54. await Task.WhenAll(tasks);
  55.  
  56. Console.WriteLine($"Completed. Success: {success}, Failed: {fail}");
  57. }
  58. }
  59.  
Success #stdin #stdout 0.05s 29532KB
stdin
Standard input is empty
stdout
Enter target URL (must be your own server): Number of requests to send: Invalid number of requests.