在C#中下载多个连接后无法读取文件 [英] Unable to read file after download multi connections in C#

查看:76
本文介绍了在C#中下载多个连接后无法读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我下载IDM的多连接机制吗?如何下载到临时文件并加入它们。

如何从互联网上下载文件 [ ^ ]我使用此代码创建一个程序,用8个连接下载一个文件。我创建了一个连接来下载一些MB(我通过使用webRequest.AddRange(开始,结束)将MB除以连接)并使用01.file,02.file,03.file保存它。下载成功但是当我加入文件时,我无法观看视频文件。请帮帮我:(

这是我的代码:

Can anyone tell me download multi connection mechanism of IDM?How it downloads to temporary files and join them.
how to download a file from internet[^] I using this code to create a program to download one file with 8 connection. I create one connection to download some MB (I divide MB for connection by using webRequest.AddRange(start,end)) and save it with 01.file, 02.file, 03.file. After download successfull but when I Join files, I can''t watch video file. Please help me :(
Here is my code:

try
 {
            HttpWebRequest webRequest;
            HttpWebResponse webResponse;
            IWebProxy proxy = null;
            webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.AddRange((int)start, (int)end);
            webRequest.Timeout = 240 * 1000;
            webRequest.Proxy = proxy; //SA??? or DefineProxy
            webResponse = (HttpWebResponse)webRequest.GetResponse();
            fs = new FileStream(
                fileName, FileMode.Append, FileAccess.Write);
            long fileLength = webResponse.ContentLength;
            Stream strm = webResponse.GetResponseStream();
            int SeekPosition = (int)start;
            Console.WriteLine(name);
            strm.ReadTimeout = 60 * 1000;
            int arrSize = 18 * 1024 * 1024; //SA???                 
            byte[] barr = new byte[arrSize];
            long bytesCounter = 0;
            while (true)
            {
                int actualBytes = 0;
                try
                {
                    actualBytes = strm.Read(barr, 0, arrSize);
                }
                catch
                {

                }
                if (actualBytes <= 0)
                {
                    if (fileLength == actualBytes)
                        break;
                    else
                    {
                        if (tryCounter == 0)
                        {
                            tryCounter++;
                            dStart = DateTime.Now;
                            continue;
                        }

                        if (dStart.AddMinutes(1) >= DateTime.Now)
                            break;
                    }
                }

                fs.Write(barr, 0, actualBytes);
                bytesCounter += actualBytes;
                Transfered = bytesCounter;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: {0}. Message: {1}", name, ex.Message);
        }
        finally
        {
            fs.Flush();
            fs.Close();
        }

推荐答案

你的循环终止条件错误:



Your loop termination condition is wrong:

if(fileLength == actualBytes)
  break;



应该是




should be

if(fileLength == bytesCounter)
  break;





除此之外,可能有不同的原因,例如不同连接的开始和结束可能以不同方式重叠或不匹配,您的摘录不包括此部分。



您是否尝试通过单个连接下载文件并将故障与工作故障进行比较,以查看文件的二进制差异?这可能会给出一些错误提示。



Apart from this, there could be different reasons, e.g. start and end of different connections could overlap or not match in different ways, your excerpt does not cover this part.

Have you tried to download the file with a single connection and compare the faulty with the working one to see the binary differences of the files? This could give some hints of what is going wrong.


这篇关于在C#中下载多个连接后无法读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆