Custom Search
Logiclabz
  • Home
  • C#
  • Download file with UserId and Password using .Net C#

Download file with UserId and Password using .Net C#

  

System.Net.NetworkCredential class in .Net provides credentials for password-based authentication schemes.

System.Net.WebClient class in .Net provides DownloadData method to download file from given URI.

System.Net.WebClient is given with the required login username and password details with System.Net.NetworkCredential.

DownloadData method gives the result byte[] of the file, which can be stored to path using System.IO.FileStream class.

WebClient wc = new WebClient();
wc.Credentials = new System.Net.NetworkCredential("username", "password");
byte[] buff = wc.DownloadData("http://www.somedomain.com/filewithlogin.zip");
using (System.IO.FileStream fs = 
new FileStream(@"C:\downloads\filewithlogin.zip", FileMode.Create))
{
      fs.Write(buff, 0, buff.Length);
      fs.Close();
}



  


Leave a reply




Do you like this post?