httpClient 下载

Posted MaxBruce

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了httpClient 下载相关的知识,希望对你有一定的参考价值。

  private void button2_Click(object sender, EventArgs e)

        {

            get();

        }

        private async Task get()

        {

            await DownloadFile(@"https://stg-gaminghub.omen.com/launchvideo/Oasis_30s_720p.mp4", "test.mp4");

            MessageBox.Show("finished..");

        }

        public async Task  DownloadFile(string serverFilePath, string targetPath)

        {

           await Task.Run(() =>

            {

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serverFilePath);

                WebResponse respone = request.GetResponse();

                Stream netStream = respone.GetResponseStream();

                using (Stream fileStream = new FileStream(targetPath, FileMode.Create))

                {

                    byte[] read = new byte[1024];

                    int realReadLen = netStream.Read(read, 0, read.Length);

                    while (realReadLen > 0)

                    {

                        fileStream.Write(read, 0, realReadLen);

                        realReadLen = netStream.Read(read, 0, read.Length);

                    }

                    netStream.Close();

                    fileStream.Close();

                }

            });

          

        }

        private async void test(string serverFilePath, string targetPath)

        {

            var client = new HttpClient();

            var netStream = await client.GetStreamAsync("");

 

            using (Stream fileStream = new FileStream(targetPath, FileMode.Create))

            {

                byte[] read = new byte[1024];

                int realReadLen = netStream.Read(read, 0, read.Length);

                while (realReadLen > 0)

                {

                    fileStream.Write(read, 0, realReadLen);

                    realReadLen = netStream.Read(read, 0, read.Length);

                }

                netStream.Close();

                fileStream.Close();

            }

        }

以上是关于httpClient 下载的主要内容,如果未能解决你的问题,请参考以下文章

httpclient下载文件

httpclient下载网页源码---java基础爬虫

[转]httpclient 上传文件下载文件

请问有org.apache.commons.httpclient.HttpClient.*的jar是哪个?该去哪儿下载

使用HttpClient实现文件的上传下载

在 Angular 6 中使用 HttpClient 下载文件