利用HttpWebRequest下载资源

简介: private void DownLoadFile() { HttpWebRequest httpWebReq = (HttpWebRequest)WebRequest.
 private void DownLoadFile()
        {
            HttpWebRequest httpWebReq = (HttpWebRequest)WebRequest.Create(address);
            httpWebReq.Method = WebRequestMethods.Http.Get;
            HttpWebResponse httpWebResp = (HttpWebResponse)httpWebReq.GetResponse();
            long contentLength = httpWebResp.ContentLength;
            this.progressBar1.Maximum = int.Parse(contentLength.ToString());

            //设置临时文件名
            string tempFileName = fileName + ".tempdl";
            //string cfgFileName = fileName + ".tempdlcfg";
            FileInfo fi = new FileInfo(tempFileName);
            if (fi.Exists)
            {
                //日后断点续传[暂时不做]
                //直接删除存在
                fi.Delete();
            }
            try
            {
                //创建临时文件 
                using (FileStream fs = fi.Create())
                {
                    fs.SetLength(contentLength);
                    //获取响应流
                    using (Stream respstm = httpWebResp.GetResponseStream())
                    {
                        if (respstm.CanRead)
                        {
                            Byte[] buffer = new byte[1024];
                            //读取数据到缓冲
                            int length = respstm.Read(buffer, 0, buffer.Length);
                            //设置进度条直
                            this.SetProgressBar(length);
                            while (length > 0)
                            {
                                //将网络流写入本地
                                fs.Write(buffer, 0, length);
                                //继续读取
                                length = respstm.Read(buffer, 0, buffer.Length);

                                //断点续传配置文件
                                //using (FileStream cfgfs = new FileStream(cfgFileName, FileMode.CreateNew))
                                //{
                                //    cfgfs.Write()
                                //}

                                //设置进度条信息
                                //this.SetProgressBar(length);
                            }
                        }
                        //写配置文件
                        //FileInfo fik12cfg = new FileInfo();
                    }
                }
                //this.ReNameApp(fi, fileName);
                //this.RunAtApp(fileName);
            }
            catch
            { }
        }

目录
相关文章
|
8月前
09JavaWeb基础 - Response类案例(下载文件)
09JavaWeb基础 - Response类案例(下载文件)
30 0
|
Web App开发 开发框架 缓存
IIS 部署网站对 OPTIONS 请求直接返回 40x 的处理
了解 OPTIONS 请求的基本功能、作用和大概拦截的原因,逐一排查,分别讲解在 asp.net (.net framework 时代)和 asp.net core (.net core/.net 时代) 的处理方式,OPTIONS 请求在不同的浏览器中默认请求行为表现不一致,通过设置 SetPreflightMaxAge (asp.net core 方式)的最大缓存时间,间接的优化...
354 0
IIS 部署网站对 OPTIONS 请求直接返回 40x 的处理
|
域名解析 网络协议 应用服务中间件
|
JSON 数据格式
HttpRequest常见问题
1、Q:HttpRequest真机请求报错误码error:4,errorMessage:无权调用该接口,和报错误码error11 A:需要把域名添加到开放平台中的小程序httpRequest接口请求域名白名单中 2, 2、Q:真机请求中如果Android请求正常,ios不能正常请求到数据请求为...
1211 0

热门文章

最新文章