Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Http;
- using System.Net.NetworkInformation;
- using System.Threading;
- using System.Threading.Tasks;
- namespace ConsoleApplication13
- {
- class Program
- {
- static void Main(string[] args)
- {
- 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" };
- var pingerTasks = addresses.Select(async address =>
- {
- using (var pinger = new Ping())
- {
- var reply = await pinger.SendPingAsync(address.Replace("http://", ""), 300);
- if (reply.Status != IPStatus.Success)
- throw new Exception();
- Console.WriteLine($"Wow, so good ping {address}");
- }
- }).ToArray();
- try
- {
- Task.WaitAll(pingerTasks);
- Console.WriteLine("УСПЕХ");
- }
- catch
- {
- Console.WriteLine("Pinger Aborted");
- }
- var cts = new CancellationTokenSource();
- var tasks = addresses.Select(async address =>
- {
- using (var httpClient = new HttpClient())
- {
- var response = await httpClient.GetAsync(address, cts.Token);
- if (!response.IsSuccessStatusCode)
- {
- Console.WriteLine($"Wow, so fail {address}: {response.StatusCode}");
- cts.Cancel();
- }
- else
- Console.WriteLine($"Wow, so good http {address}");
- }
- }).ToArray();
- try
- {
- Task.WaitAll(tasks);
- Console.WriteLine("УСПЕХ");
- }
- catch
- {
- Console.WriteLine("HttpClient Aborted");
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment