fcamuso

Networking, video 122 e 123

Sep 3rd, 2021 (edited)
1,237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net.Http;
  5. using System.Net.Http.Headers;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8.  
  9. namespace Networking_B
  10. {
  11.   class Program
  12.   {
  13.     static async Task ScaricaConHTTPClient()
  14.     {
  15.  
  16.       using HttpClient client = new ();
  17.       var rispostaServer = await client.GetAsync("https://www.camuso.it/DA_CANCELLARE/test.pdf");
  18.       using var fs = new FileStream("test.pdf", FileMode.Create);
  19.       await rispostaServer.Content.CopyToAsync(fs);
  20.  
  21.       Thread.Sleep(5000);
  22.       Console.WriteLine("Download completato ...");
  23.      }
  24.  
  25.     static async Task<HttpContent> UploadDatiForm()
  26.     {
  27.       var client = new HttpClient();
  28.       var campi = new Dictionary<string, string> {
  29.         { "idArticolo", "XY009PQ" },
  30.         { "descrizione", "manicotto XL" } };
  31.      
  32.       var datiForm = new FormUrlEncodedContent(campi);
  33.       var rispostaServer= await client.PostAsync("http://localhost/php/testuploadYT/form.php", datiForm);
  34.       rispostaServer.EnsureSuccessStatusCode();
  35.       return rispostaServer.Content;
  36.     }
  37.  
  38.     private static async Task<HttpContent> UploadImage()
  39.     {
  40.  
  41.       HttpClient client = new HttpClient();
  42.  
  43.       //la form
  44.       MultipartFormDataContent form = new MultipartFormDataContent();
  45.  
  46.         //il field immagine dentro la form
  47.         FileStream f = File.OpenRead("fido.png");
  48.         HttpContent field_immagine = new StreamContent(f);
  49.  
  50.         field_immagine.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
  51.         {
  52.           Name = "nome_file",
  53.           FileName = f.Name
  54.         };
  55.  
  56.       form.Add(field_immagine);
  57.  
  58.       var rispostaServer = await client.PostAsync("http://localhost/php/testuploadYT/upload.php", form);
  59.       rispostaServer.EnsureSuccessStatusCode();
  60.  
  61.       return rispostaServer.Content;
  62.     }
  63.  
  64.  
  65.     static async Task Main()
  66.     {
  67.       //await ScaricaConHTTPClient();
  68.  
  69.       //upload dati a una form
  70.       //HttpContent rispostaServer = await UploadDatiForm();
  71.       //Console.WriteLine(await rispostaServer.ReadAsStringAsync());
  72.  
  73.       HttpContent rispostaServer = await UploadImage();
  74.       Console.WriteLine(await rispostaServer.ReadAsStringAsync());
  75.  
  76.     }
  77.   }
  78. }
  79.  
Add Comment
Please, Sign In to add comment