Guest User

Untitled

a guest
Oct 21st, 2016
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Http;
  5. using System.Net.NetworkInformation;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8.  
  9. namespace ConsoleApplication13
  10. {
  11.     class Program
  12.     {
  13.        
  14.         static void Main(string[] args)
  15.         {
  16.             var addresses = new List<string> { "http://google.ru", "http://yandex.ru", "http://rambler.ru", "http://bing.com", "http://ridus.ru", "http://habrahabr.ru", "http://geektimes.ru", "http://mail.ru", "http://2ch.hk", "http://iichan.hk", "http://dobrochan.ru", "http://youtube.com", "http://postnauka.ru", "http://nplus1.ru", "http://elementy.ru", "http://membrana.ru", "http://antropogenez.ru", "http://histrf.ru", "http://iddqd.ru" };
  17.  
  18.             var pingerTasks = addresses.Select(async address =>
  19.             {
  20.                 using (var pinger = new Ping())
  21.                 {
  22.                     var reply = await pinger.SendPingAsync(address.Replace("http://", ""), 300);
  23.                     if (reply.Status != IPStatus.Success)
  24.                         throw new Exception();
  25.  
  26.                         Console.WriteLine($"Wow, so good ping {address}");
  27.                 }
  28.             }).ToArray();
  29.  
  30.             try
  31.             {
  32.                 Task.WaitAll(pingerTasks);
  33.                 Console.WriteLine("УСПЕХ");
  34.             }
  35.             catch
  36.             {
  37.                 Console.WriteLine("Pinger Aborted");
  38.             }
  39.  
  40.  
  41.             var cts = new CancellationTokenSource();
  42.             var tasks = addresses.Select(async address =>
  43.             {
  44.                 using (var httpClient = new HttpClient())
  45.                 {
  46.                     var response = await httpClient.GetAsync(address, cts.Token);
  47.                     if (!response.IsSuccessStatusCode)
  48.                     {
  49.                         Console.WriteLine($"Wow, so fail {address}: {response.StatusCode}");
  50.                         cts.Cancel();
  51.                     }
  52.                     else
  53.                         Console.WriteLine($"Wow, so good http {address}");
  54.                 }
  55.             }).ToArray();
  56.  
  57.             try
  58.             {
  59.                 Task.WaitAll(tasks);
  60.                 Console.WriteLine("УСПЕХ");
  61.             }
  62.             catch
  63.             {
  64.                 Console.WriteLine("HttpClient Aborted");
  65.             }
  66.             Console.ReadKey();
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment