VB.NET-APP Downloading file using httpwebrequest/response

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raids51
    New Member
    • Nov 2007
    • 59

    VB.NET-APP Downloading file using httpwebrequest/response

    Hello, i have a program that downloads a file using the httpwebrequest/response, and it usually works, but sometimes it will freeze at a random part of the download without an error.
    here is the code:
    Code:
      'Creating the request and getting the response
            Dim theResponse As HttpWebResponse
            Dim theRequest As HttpWebRequest
    
            Try 'Checks if the file exist
                theRequest = WebRequest.Create(Me.Filelocation)
                theResponse = theRequest.GetResponse
            Catch ex As Exception
    
                MessageBox.Show("An error occurred while downloading file. Possibe causes:" & ControlChars.CrLf & _
                                "1) File doesn't exist" & ControlChars.CrLf & _
                                "2) Remote server error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    
                Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
    
                Me.Invoke(cancelDelegate, True)
    
                Exit Sub
            End Try
            theRequest.Timeout = 30000
            theRequest.KeepAlive = True
            Dim length As Long = 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(Me.whereToSave, 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 Integer = (nRead / length) * 100
    
                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()
    
            If Me.BackgroundWorker1.CancellationPending Then
    
                IO.File.Delete(Me.whereToSave)
    
                Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
    
                Me.Invoke(cancelDelegate, True)
    
                Exit Sub
    
            End If
    
            Dim completeDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
            Try
                Me.Invoke(completeDelegate, False)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
    But im pretty sure my problem is in this part of the code:

    Code:
               
     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 Integer = (nRead / length) * 100
    
                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
  • joedeene
    Contributor
    • Jul 2008
    • 579

    #2
    Put a Try...Catch Statement around the WHOLE code, and then in the 'Catch Ex as Exception' line, under that have a message box showing the whole error.

    Example:

    Code:
          Try
            'All Code
            Catch ex As Exception
                MessageBox.Show(ex.ToString())
            End Try
    This should give you an error the error that's happening some point in the code, post back the error and line number so we can help you furthermore. :)

    joedeene

    Comment

    • raids51
      New Member
      • Nov 2007
      • 59

      #3
      i tried that already, but it doesnt show that anythings wrong, the whole program still works but the downloaded freezes at a random place... and it doesnt do it all the time it, its just random.

      Comment

      • joedeene
        Contributor
        • Jul 2008
        • 579

        #4
        Hmm...This has occurred to me a while ago when I was working with downloading files, but since you're in VB.NET, why not use the My.Computer.Net work.DownloadFi le() Method, it's kind of the lazy way, but if it works?

        joedeene

        Comment

        • raids51
          New Member
          • Nov 2007
          • 59

          #5
          because i want to get the speed and progress of the download

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Hmm these lines:
            theRequest.Time out = 30000
            theRequest.Keep Alive = True
            have no effect since you have already called for the response.

            Your code does lot of work, is it possible it is getting stuck in a loop somewhere.
            Have you set a breakpoint and stepped through your code?

            Comment

            • raids51
              New Member
              • Nov 2007
              • 59

              #7
              its usually just a random point that it just decides to stop downloading, i did find a quick fix though... instead of using a backgroundworke r i just put it all in a different sub and called application.doe vents, and this seemed to fix it.

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                That was as I thought.
                You are in a busy loop somewhere trying to get the rest of the data, but the managed object doesn't know more data has come in, because it never had the chance to get its messages (the doevents() call)

                Comment

                Working...