Download File in Chunks

CarcaBot

Well-known member
Joined
Apr 23, 2007
Messages
47
Location
Romania
Programming Experience
Beginner
Hello,


3 days i'm trying unsuccessful to download a file in chunks.
I've searched in every corner of google page and no success.

What i want to do is that i'm trying to download the resulted link from here http://188.173.28.235/mylink.html
(be carefully that this link can be loaded only once in browser, then refresh page to get the new link.)

I made this to work in php but in vb.net i can't.
Actually i'm using httpwebrequest , i've set SendChunked = true and i receive an error for GetResponse.

thanks in advance.
 
PHP:
[...]
Dim theResponse As HttpWebResponse
        Dim theRequest As HttpWebRequest
        Dim newlink As String

      theRequest = WebRequest.Create(newlink)
        theRequest.SendChunked = True
        theRequest.AllowWriteStreamBuffering = True
        theRequest.AllowAutoRedirect = True
        theResponse = theRequest.GetResponse
        Dim length As Long = Convert.ToInt32(theResponse.ContentLength) 'Size of the response (in bytes)
            Dim safedelegate As New ChangeTextsSafe(AddressOf ChangeTexts)
            Me.Invoke(safedelegate, length, 0, 0, 0) 'Invoke the TreadsafeDelegate

            Dim writeStream As New IO.FileStream(My.Settings.numefisier, IO.FileMode.Create)

            'Replacement for Stream.Position (webResponse stream doesn't support seek)
            Dim nRead As Integer

            'To calculate the download speed
            Dim speedtimer As New Stopwatch
            Dim currentspeed As Double = -1
            Dim readings As Integer = 0
         Do

                If BackgroundWorker1.CancellationPending Then 'If user abort download
                    Exit Do
                End If

                speedtimer.Start()
                Dim readBytes(4095) As Byte
                Dim bytesread As Integer = theResponse.GetResponseStream.Read(readBytes, 0, 4096)

                nRead += bytesread
                Dim percent As Short = (Convert.ToInt64(nRead) * 100) / Convert.ToInt64(length)

                Me.Invoke(safedelegate, length, nRead, percent, currentspeed)

                If bytesread = 0 Then Exit Do

                writeStream.Write(readBytes, 0, bytesread)

                speedtimer.Stop()

                readings += 1
                If readings >= 5 Then 'For increase precision, the speed it's calculated only every five cicles
                    currentspeed = 20480 / (speedtimer.ElapsedMilliseconds / 1000)
                    speedtimer.Reset()
                    readings = 0
                End If

            Loop

            'Close the streams
            theResponse.GetResponseStream.Close()
            writeStream.Close()
[...]

This is the part which downloading file.
 
Back
Top