fcamuso

Networking, video 124

Sep 10th, 2021 (edited)
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Net.Http.Headers;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9.  
  10. namespace NetworkingC
  11. {
  12.   class Program
  13.   {
  14.     static void GoogleSearchWebClient(string stringaRicerca)
  15.     {
  16.       var client = new System.Net.WebClient { Proxy = null };
  17.       client.QueryString.Add("q", stringaRicerca);
  18.  
  19.       client.DownloadString("http://www.google.com/search");
  20.      
  21.       client.DownloadFile ("http://www.google.com/search", "results.html");
  22.     }
  23.  
  24.     static void DownloadFTP()
  25.     {
  26.       //string[] credenziali = File.ReadAllLines("g:\\cred.txt");
  27.  
  28.       string username = "vostra_user_name"; //credenziali[0];
  29.       string password = "vostra_password"; //credenziali[1];
  30.  
  31.       // oggetto con cui comunicare con il server
  32.       var richiestaFTP = (FtpWebRequest)WebRequest.Create("ftp://ftp.camuso.it");
  33.       richiestaFTP.Proxy = null;
  34.  
  35.       //tipo operazione richiesta
  36.       richiestaFTP.Method = WebRequestMethods.Ftp.ListDirectory;
  37.       // credenziali
  38.       richiestaFTP.Credentials = new System.Net.NetworkCredential(username, password);
  39.  
  40.       using (WebResponse rispostaServer = richiestaFTP.GetResponse())
  41.       using (StreamReader stream = new (rispostaServer.GetResponseStream()))
  42.       Console.WriteLine(stream.ReadToEnd());
  43.  
  44.       //return;
  45.  
  46.       //upload file
  47.       richiestaFTP = (FtpWebRequest)WebRequest.Create("ftp://ftp.camuso.it/DA_CANCELLARE/clip/test.txt");
  48.       richiestaFTP.Method = WebRequestMethods.Ftp.UploadFile;
  49.       richiestaFTP.Credentials = new System.Net.NetworkCredential(username, password);
  50.  
  51.       byte[] sorgente;
  52.       //creare preventivamente nella cartella dell'eseguibile un file "testLocale.txt"
  53.       using (StreamReader stream = new ("testLocale.txt"))
  54.       {
  55.         sorgente = System.Text.Encoding.UTF8.GetBytes(stream.ReadToEnd());
  56.       }
  57.  
  58.       richiestaFTP.ContentLength = sorgente.Length;
  59.       richiestaFTP.Proxy = null;
  60.       //richiestaFTP.AuthenticationLevel = System.Net.Security.AuthenticationLevel.None;
  61.       //richiestaFTP.PreAuthenticate = true;
  62.       //richiestaFTP.UseBinary = true;
  63.       //richiestaFTP.UsePassive = true;                            
  64.  
  65.       using (Stream requestStream = richiestaFTP.GetRequestStream())
  66.       {
  67.         requestStream.Write(sorgente, 0, sorgente.Length);
  68.       }
  69.  
  70.       using FtpWebResponse response = (FtpWebResponse)richiestaFTP.GetResponse();
  71.       Console.WriteLine($"Upload File Completato, stato: {response.StatusDescription}");
  72.     }  
  73.      
  74.      
  75.     static void Main(string[] args)
  76.     {
  77.       //GoogleSearchWebClient("cani e gatti");
  78.       DownloadFTP();
  79.     }
  80.   }
  81. }
  82.  
Add Comment
Please, Sign In to add comment