Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2016
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.01 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Net.Http;
  4. using System.Threading;
  5.  
  6. namespace HttpClientTest
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             while (Check())
  13.             {
  14.                 Console.WriteLine("can't start still here");
  15.                 Thread.Sleep(10);
  16.             }
  17.  
  18.             Console.WriteLine("Starting connections");
  19.             for (int i = 0; i < 10; i++)
  20.             {
  21.                 using (var client = new HttpClient())
  22.                 {
  23.                     //client.DefaultRequestHeaders.Connection.Add("close");
  24.                     var result = client.GetAsync("http://aspnetmonsters.com").Result;
  25.                     Console.WriteLine(result.StatusCode);
  26.                 }
  27.             }
  28.             Console.WriteLine("Connections done");
  29.  
  30.             var start = DateTime.Now;
  31.  
  32.             while (Check())
  33.             {
  34.                 Console.Write(".");
  35.                 Thread.Sleep(10);
  36.             }
  37.  
  38.             //not the best benchmark but anyway
  39.             var end = DateTime.Now;
  40.  
  41.             Console.WriteLine();
  42.             Console.WriteLine("total time: {0:N} milliseconds", (int)end.Subtract(start).TotalMilliseconds);
  43.  
  44.             Console.ReadKey();
  45.         }
  46.  
  47.         private static bool Check()
  48.         {
  49.             var proc = new Process
  50.             {
  51.                 StartInfo = new ProcessStartInfo
  52.                 {
  53.                     FileName = "netstat.exe",
  54.                     Arguments = "-n -p TCP",
  55.                     UseShellExecute = false,
  56.                     RedirectStandardOutput = true,
  57.                     CreateNoWindow = true
  58.                 }
  59.             };
  60.  
  61.             proc.Start();
  62.             while (!proc.StandardOutput.EndOfStream)
  63.             {
  64.                 string line = proc.StandardOutput.ReadLine();
  65.                 if (line != null && line.Contains("23.101.203.214")) return true;
  66.             }
  67.  
  68.             return false;
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement