C#开发学习笔记:利用WebClient下载文件

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;//设置对象允许的最大连接数

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值