Webclient

using System;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            TestWebclient testWebclient = new TestWebclient();
            testWebclient.WebClientUpload();
            //WebClientDownload();
            //WebClientUpload();
            //WebClientDelete();
            Console.ReadKey();
        }

        #region 下载
        /// <summary>
        /// 下载
        /// </summary>
        static void WebClientDownload()
        {
            WebClient webClient = new WebClient
            {
                Credentials = CredentialCache.DefaultCredentials
            };
            //Uri _uri = new Uri(@"http://localhost:8082/123.txt");
            Uri uri = new Uri(@"http://192.168.0.100:8082/123.txt");
            webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
            webClient.DownloadFileCompleted += WebClient_DownloadFileCompleted;
            webClient.DownloadFileAsync(uri, @"D:\download\123.txt");
        }

        private static void WebClient_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            Console.WriteLine("下载完成...");
        }

        private static void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            Console.WriteLine($"{e.ProgressPercentage}:{e.BytesReceived}/{e.TotalBytesToReceive}");
        }

        #endregion

        #region 上传

        /// <summary>
        /// 上传
        /// </summary>
        static void WebClientUpload()
        {
            WebClient webClient = new WebClient
            {
                Credentials = new NetworkCredential("test", "123")
            };
            Uri uri = new Uri(@"http://192.168.0.100:8082/456.xlsx");
            webClient.UploadProgressChanged += WebClient_UploadProgressChanged;
            webClient.UploadFileCompleted += WebClient_UploadFileCompleted;
            webClient.UploadFileAsync(uri, "PUT", @"D:\download\456.xlsx");
        }

        private static void WebClient_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e)
        {
            Console.WriteLine("上传完成...");
        }

        private static void WebClient_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
        {
            Console.WriteLine($"{e.ProgressPercentage}:{e.BytesSent}/{e.TotalBytesToSend}");
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        static void WebClientDelete()
        {
            WebClient webClient = new WebClient
            {
                Credentials = new NetworkCredential("test", "123")
            };
            Uri uri = new Uri(@"http://192.168.0.100:8082/456.xlsx");           
            webClient.UploadDataCompleted += WebClient_UploadDataCompleted;
            webClient.UploadDataAsync(uri, "DELETE", new byte[0]);
        }

        private static void WebClient_UploadDataCompleted(object sender, UploadDataCompletedEventArgs e)
        {
            Console.WriteLine("已删除...");
        }
        #endregion
    }

}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值