Mr_WTX
2022-03-03 15:08
采纳率: 50%
浏览 15

C# wpf下载问题,有没有解答一下。C# wpf有这么坑嘛?

使用httpwebrequest下载视频文件速度慢,无法打开播放。以及webclient下载速度慢,会中断致使文件下载不完整。
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                        //byte[] byteArr = webClient.DownloadData(url);
                        //string contentEncoding = webClient.ResponseHeaders["Content-Encoding"];
                        if (response.ContentEncoding == "gzip")
                        {
                            //Stream stream = new MemoryStream(byteArr);
                            Stream stream = response.GetResponseStream();
                            using (Stream zipStream = new GZipStream(stream, CompressionMode.Decompress))
                            {
                                using (Stream fileStream = new FileStream(address, FileMode.Create))
                                {
                                    byte[] read = new byte[1024];
                                    int readlen = zipStream.Read(read, 0, read.Length);
                                    while (readlen > 0)
                                    {
                                        fileStream.Write(read, 0, read.Length);
                                        readlen = zipStream.Read(read, 0, read.Length);
                                    }
                                    //fileStream.Close();
                                    //zipStream.Close();
                                    //stream.Close();
                                }
                            }
                            stream.Close();
                        }
                        else
                        {
                            webClient.DownloadFile(new Uri(url), address);
                            //Stream stream = new MemoryStream(byteArr);
                            //Stream stream = response.GetResponseStream();
                            //stream.ReadTimeout = -1;
                            //using (FileStream fileStream = new FileStream(address, FileMode.Create))
                            //{
                            //    byte[] read = new byte[1024];
                            //    int readlen = stream.Read(read, 0, read.Length);
                            //    while (readlen > 0)
                            //    {
                            //        fileStream.Write(read, 0, read.Length);
                            //        readlen = stream.Read(read, 0, read.Length);
                            //    }
                            //    //fileStream.Close();
                            //}
                            //stream.Close();
                        }

使用webclient下载视频文件出现速度慢,下载视频文件不完整的问题。突然结束中断。使用httpwebrequest下载的视频打不开,虽然下载的视频文件不是完整的。(webclient下载的不完整但是可以播放)
我的解答思路和尝试过的方法
我想要达到的结果

2条回答 默认 最新

相关推荐 更多相似问题