使用WebClient类对网页下载源码,对文件下载保存及异步下载并报告下载进度

本文介绍了一个简单的C#程序示例,演示了如何使用WebClient类从互联网上下载字符串数据、文件,并且实现异步下载过程中的进度显示。

 

private void button1_Click(object sender, EventArgs e)
        {
            WebClient webclient = new WebClient();
            webclient.Proxy = null;
            webclient.Encoding = Encoding.UTF8;
            richTextBox1.AppendText(webclient.DownloadString(textBox1.Text.Trim()));
            webclient.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            WebClient webclient = new WebClient();
            webclient.Proxy = null;
            webclient.DownloadFile(textBox2.Text, textBox3.Text);
            MessageBox.Show("成功!");
            webclient.Dispose();

        }

        private void button3_Click(object sender, EventArgs e)
        {
            WebClient webclient = new WebClient();
            webclient.Proxy = null;
            webclient.DownloadProgressChanged += webclient_DownloadProgressChanged;
            webclient.DownloadDataCompleted += webclient_DownloadDataCompleted;
            webclient.DownloadFileAsync(new Uri(textBox2.Text), textBox3.Text);
            webclient.Dispose();
        }

        void webclient_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
        {
            MessageBox.Show("成功");
        }

        void webclient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }

 

转载于:https://www.cnblogs.com/lunawzh/p/5540589.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值