Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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();
        }
Posted
v2

1 solution

Your loop termination condition is wrong:

C#
if(fileLength == actualBytes)
  break;

should be

C#
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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900