Mega Code Archive

 
Categories / C# Book / 08 Net
 

0587 Authentication

You can supply a username and password to an HTTP or FTP site by creating a NetworkCredential object and assigning it to the Credentials property of WebClient or WebRequest: using System; using System.Net; using System.IO; using System.Linq; using System.Text; class Program { static void Main() { WebClient wc = new WebClient(); wc.Proxy = null; wc.BaseAddress = "ftp://ftp.abc.com"; // Authenticate, then upload and download a file to the FTP server. // The same approach also works for HTTP and HTTPS. string username = "nutshell"; string password = "yourValue"; wc.Credentials = new NetworkCredential(username, password); wc.DownloadFile("guestbook.txt", "guestbook.txt"); string data = "Hello from " + Environment.UserName + "!\r\n"; File.AppendAllText("guestbook.txt", data); wc.UploadFile("guestbook.txt", "guestbook.txt"); } }