Advertisement
fcamuso

Networking, video 121

Aug 27th, 2021
1,468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.24 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Net.Http;
  4. using System.Runtime.CompilerServices;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Networking_A
  9. {
  10.   class Program
  11.   {
  12.     static ManualResetEvent mre = new ManualResetEvent(false);
  13.    
  14.     static void FaiAltro()
  15.     {
  16.       for (int i = 0; i < 100; i++)
  17.         Console.WriteLine("Lavoro ...");
  18.  
  19.       //Thread.Sleep(25000);
  20.     }
  21.  
  22.     static async Task Scarica(WebClient client)
  23.     {
  24.       client.DownloadProgressChanged += (sender, args) =>
  25.         Console.Write($"\r{args.ProgressPercentage}%  ricevuti: {args.BytesReceived}/{args.TotalBytesToReceive}");
  26.  
  27.       await client.DownloadFileTaskAsync("https://www.camuso.it/DA_CANCELLARE/clip.mp4", "clip.mp4");
  28.  
  29.       Console.WriteLine("\nDownload completato");
  30.       mre.Set();
  31.  
  32.     }
  33.  
  34.     static async Task ScaricaConHTTPClientToken()
  35.     {
  36.       using (HttpClient httpClient = new HttpClient())
  37.       {
  38.         CancellationTokenSource cts = new CancellationTokenSource();
  39.         cts.CancelAfter(10000);
  40.  
  41.         httpClient.Timeout = TimeSpan.FromSeconds(5);
  42.  
  43.         string homePage = "";
  44.         try
  45.         {
  46.           homePage = await httpClient.GetStringAsync("https://www.camuso.it:81", cts.Token);
  47.         }
  48.         catch (TaskCanceledException eccezione)
  49.         {
  50.           if (eccezione.InnerException is TimeoutException)
  51.             Console.WriteLine("Timeout scaduto ...");
  52.           else
  53.             Console.WriteLine("Download abortito su richiesta dell'utente");
  54.         }
  55.         finally
  56.         {
  57.           Console.WriteLine(homePage);
  58.           mre.Set();
  59.         }
  60.       }
  61.     }
  62.  
  63.     static async Task ScaricaConHTTPClient()
  64.     {
  65.       using (HttpClient httpClient = new HttpClient())
  66.       {
  67.         //httpClient.Timeout = TimeSpan.FromSeconds(5);
  68.  
  69.         string homePage = "";
  70.         try
  71.         {
  72.           homePage = await httpClient.GetStringAsync("https://www.camuso.it");
  73.           Console.WriteLine(homePage);
  74.         }
  75.         catch (TaskCanceledException eccezione) when (eccezione.InnerException is TimeoutException)
  76.         {
  77.           Console.WriteLine("Timeout scaduto ...");
  78.         }
  79.         finally {
  80.           mre.Set();  
  81.         }
  82.        
  83.  
  84.       }
  85.     }
  86.  
  87.     static void Main(string[] args)
  88.     {
  89.       //Download di un file, senza reporting
  90.       WebClient client = new () { Proxy = null };
  91.       //client.DownloadFile("https://www.camuso.it/index.asp", "index.asp");
  92.  
  93.       //try
  94.       //{
  95.       //  client.DownloadFile("https://www.camuso.it/cicli2.jpg", "cicli2.jpg");
  96.       //}
  97.       //catch (WebException e)
  98.       //{
  99.       //  Console.WriteLine("Errore 404: risorsa non trovata");
  100.       //}
  101.  
  102.       //scaricamento in asincrono con possibilità di abortire
  103.       //var t =Scarica(client);
  104.       //Console.WriteLine("Io intanto continuo...");
  105.  
  106.       //Task task = Task.Factory.StartNew(() => FaiAltro());
  107.       //task.Wait();
  108.  
  109.       //Thread.Sleep(5000);
  110.       //Console.Clear();
  111.       //Console.WriteLine("Download interrotto dall'utente ...");
  112.       //client.CancelAsync();
  113.       //mre.Set();
  114.  
  115.       //var t = ScaricaConHTTPClient();
  116.       var t = ScaricaConHTTPClientToken();
  117.       mre.WaitOne();
  118.  
  119.     }
  120.   }
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement