Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 Frown | :(
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();
        }
Posted 19-Mar-13 22:24pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 381
1 Sergey Alexandrovich Kryukov 245
2 Marcin Kozub 225
3 Praneet Nadkar 217
4 /\jmot 189
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,614
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 20 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100