Friday, October 10, 2008

Easy File Download from web

Its a single line code to download a file given a url and downlaod dir

(new WebClient()).DownloadFile(url, downlaoddir);

But if there is windows authentication required on the site the maybe three lines :)

WebClient client = new WebClient();
client.UseDefaultCredentials = true;
client.DownloadFile(url, downloaddir);

But it seems to a very easy option to work with.
It also provides multiple async downloads etc.

Ex:

WebClient wc = new WebClient();
string website1 = "http://remote1.ispnet.net";
string website2 = "http://remote2.ispnet.net";
string website3 = "http://remote3.ispnet.net/login";


NetworkCredential nc1 = new NetworkCredential("mike", "guitars");
NetworkCredential nc2 = new NetworkCredential("evonne", "singing", "home");
NetworkCredential nc3 = new NetworkCredential("alex", "drums");

CredentialCache cc = new CredentialCache();
cc.Add(new Uri(website1), "Basic", nc1);
cc.Add(new Uri(website2), "Basic", nc2);
cc.Add(new Uri(website3), "Digest", nc3);

wc.Credentials = cc;

wc.DownloadFile(website1, "website1.htm");
wc.DownloadFile(website2, "website2.htm");
wc.DownloadFile(website3, "website3.htm");

form - Example Reference

No comments: