Advertisement:

Skystone Software

http://www.SkystoneSoftware.com

Scott Waletzko's Blog
Downloading Web Files with Authentication
Published: 10/28/2008
XMl / RSS

Today's programs are almost always Web-enabled in some fashion, and the Web has become the defacto vehicle for distributing data. There may be times when you find the need to download a file from a Web server, and most often times the file will be protected by some sort of authentication method. Here's some quick code that will enable this:

VB:
Dim username As String = "type your username here" 
Dim password As String = "type your password here" 
Dim url As String = "type url to picture here" 
Dim localPath As String = "type path you want to save the picture to here" 
  
Dim client As New WebClient() 
client.Credentials = New NetworkCredential(username, password)
client.DownloadFile(url, localPath) 	
	
C#:
string username = "type your username here"; 
string password = "type your password here"; 
string url = "type url to picture here"; 
string localPath = "type path you want to save the picture to here"; 
  
WebClient client = new WebClient(); 
client.Credentials = new NetworkCredential(username, password); 
client.DownloadFile(url, localPath); 
	



Questions or Comments? .

VB to C# and C# to VB translation provided by Instant C# and Instant VB.