zoukankan      html  css  js  c++  java
  • 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();

                }

            }

  • 相关阅读:
    ChinaCock界面控件介绍-CCLoadingIndicator
    delphi-search-path-vs-library-path-vs-browsing-path
    10.3制作Android Splash启动界面
    FMX取得屏分辨率
    REST easy with kbmMW #20 – OpenAPI and Swagger UI
    升级ChinaCock 10.3遇到的问题
    FastJson中的ObjectMapper对象的使用详解
    fastjson的值过滤器ValueFilter
    springboot 2.0 配置 spring.jackson.date-format 不生效
    War 包部署
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14904932.html
Copyright © 2011-2022 走看看