1.实例化一个WebClient对象
WebClient myClient = new WebClient();
2.设置主机访问需要的用户凭证
NetworkCredential Credential = new NetworkCredential("用户账号", "用户密码");
myClient.Credentials = Credential;
注:如果未设置相关报错:远程服务器返回错误(401)未经授权
如果是直接访问类似svn服务器则凭据是访问svn时所用的服务器,如果直接访问的是服务器虚拟文件路径,则应该是电脑访问凭据
3.解决网站证书不正确
System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(RemoteCertificateValidationCallback);
public static bool RemoteCertificateValidationCallback(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors errors)
{
return true;
}
注:如果未设置相关报错:基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系的问题
4.下载文件内容(主要是为了查看能否正确下载内容)
//下载目标文件数据
byte[] data = myClient.DownloadData("服务器路径/Updateinfo.xml");
//将字节转为string类型
string getdata = Encoding.UTF8.GetString(data);
服务器路径/Updateinfo.xml");
//将字节转为string类型
string getdata = Encoding.UTF8.GetString(data);
5.执行下载
Uri uri = new Uri("服务器路径/Updateinfo.xml");
myClient.DownloadFileAsync(uri, "本地保存路径\\Updateinfo.xml");//异步下载
6.完整代码
WebClient myClient = new WebClient();
myClient.DownloadFileCompleted += new AsyncCompletedEventHandler(MyWebClient_DownloadFileCompleted);//下载完成执行的事件
myClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(MyWebClient_DownloadProgressChanged);//进度条
//设置主机访问需要用户凭证(如果未设置相关错误:远程服务器返回错误(401)未经授权)
NetworkCredential Credential = new NetworkCredential("用户名", "用户密码");
myClient.Credentials = Credential;
//解决基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系的问题(该问题由于网站证书不正确引起)
System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(RemoteCertificateValidationCallback);
Uri uri = new Uri("服务器路径/Updateinfo.xml");
//下载目标文件数据byte[] data = myClient.DownloadData("服务器路径/Updateinfo.xml");
//将字节转为string类型string getdata = Encoding.UTF8.GetString(data); myClient.DownloadFileAsync(uri, "本地保存路径\\Updateinfo.xml");//异步下载
private void MyWebClient_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show(e.Error.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if ((e.Cancelled == true))
{
MessageBox.Show("下载文件操作被取消!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("下载文件操作完成!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void MyWebClient_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
{
this.progressBar1.Value = e.ProgressPercentage;
this.label1.Text = "已经下载" + e.BytesReceived.ToString() + " 字节,全部共有" + e.TotalBytesToReceive.ToString() + "字节";
}
注:
1.由于文件存放于svn服务器(远程服务器),一开始遇见下载文件的内容不正确;可能的解决方案(不确定后续对服务器的操作是否是解决方法):由于一开始服务器未装IIS服务,在装上IIS服务之后下载正常
2.异步下载(DownloadFileAsync)时,由于未知原因,调试时发现执行完DownloadFileAsync这一步之后下载的文件都为0字节,继续往下执行代码,过一段时间发现文件下载完成
下载速度性能优化
WebClient对象实例化之前添加
ServicePointManager.UseNagleAlgorithm = true;
ServicePointManager.Expect100Continue = true;
ServicePointManager.DefaultConnectionLimit = 512;//设置对象允许的最大连接数