使用C#下载带密码和用户名的文件

时间:2011-05-17 02:43:43

标签: c# download

如何编写脚本以从此站点下载文件。是否可以使用URL提供登录名和密码?

http://feeds.itunes.apple.com/feeds/epf/

我会像这样格式化网址吗?

WebClient Client = new WebClient();
Client.DownloadFile("http://feeds.itunes.apple.com/feeds/epf/v3/full/current/itunes20110511.tbz.md5?username=myusername&password=mypassword", @"C:\folder\file.md5");

2 个答案:

答案 0 :(得分:16)

是的,只需使用用户名/密码将WebClient's Credentials property设置为NetworkCredentials个实例即可。例如:

Client.Credentials = new System.Net.NetworkCredential("john", "password1234!");

答案 1 :(得分:15)

使用WebClient.Credentials属性为您的网站提供凭据:

using (WebClient client = new WebClient()) {
    client.Credentials = new NetworkCredential(username, password);
    client.DownloadFile("http://feeds.itunes.apple.com/feeds/epf/v3/full/current/itunes20110511.tbz.md5", @"C:\folder\file.md5");
}