Connecting Tech Pros Worldwide Forums | Help | Site Map

VB.NET-APP Downloading file using httpwebrequest/response

Member
 
Join Date: Nov 2007
Location: Alberta, Canada
Posts: 59
#1: Nov 9 '08
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:
Expand|Select|Wrap|Line Numbers
  1.   'Creating the request and getting the response
  2.         Dim theResponse As HttpWebResponse
  3.         Dim theRequest As HttpWebRequest
  4.  
  5.         Try 'Checks if the file exist
  6.             theRequest = WebRequest.Create(Me.Filelocation)
  7.             theResponse = theRequest.GetResponse
  8.         Catch ex As Exception
  9.  
  10.             MessageBox.Show("An error occurred while downloading file. Possibe causes:" & ControlChars.CrLf & _
  11.                             "1) File doesn't exist" & ControlChars.CrLf & _
  12.                             "2) Remote server error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  13.  
  14.             Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
  15.  
  16.             Me.Invoke(cancelDelegate, True)
  17.  
  18.             Exit Sub
  19.         End Try
  20.         theRequest.Timeout = 30000
  21.         theRequest.KeepAlive = True
  22.         Dim length As Long = theResponse.ContentLength 'Size of the response (in bytes)
  23.  
  24.         Dim safedelegate As New ChangeTextsSafe(AddressOf ChangeTexts)
  25.         Me.Invoke(safedelegate, length, 0, 0, 0) 'Invoke the TreadsafeDelegate
  26.  
  27.         Dim writeStream As New IO.FileStream(Me.whereToSave, IO.FileMode.Create)
  28.  
  29.         'Replacement for Stream.Position (webResponse stream doesn't support seek)
  30.         Dim nRead As Integer
  31.  
  32.         'To calculate the download speed
  33.         Dim speedtimer As New Stopwatch
  34.         Dim currentspeed As Double = -1
  35.         Dim readings As Integer = 0
  36.  
  37.         Do
  38.             If BackgroundWorker1.CancellationPending Then 'If user abort download
  39.                 Exit Do
  40.             End If
  41.  
  42.             speedtimer.Start()
  43.  
  44.             Dim readBytes(4095) As Byte
  45.             Dim bytesread As Integer = theResponse.GetResponseStream.Read(readBytes, 0, 4096)
  46.  
  47.             nRead += bytesread
  48.             Dim percent As Integer = (nRead / length) * 100
  49.  
  50.             Me.Invoke(safedelegate, length, nRead, percent, currentspeed)
  51.  
  52.             If bytesread = 0 Then Exit Do
  53.  
  54.             writeStream.Write(readBytes, 0, bytesread)
  55.  
  56.             speedtimer.Stop()
  57.  
  58.             readings += 1
  59.             If readings >= 5 Then 'For increase precision, the speed it's calculated only every five cicles
  60.                 currentspeed = 20480 / (speedtimer.ElapsedMilliseconds / 1000)
  61.                 speedtimer.Reset()
  62.                 readings = 0
  63.             End If
  64.         Loop
  65.  
  66.         'Close the streams
  67.         theResponse.GetResponseStream.Close()
  68.         writeStream.Close()
  69.  
  70.         If Me.BackgroundWorker1.CancellationPending Then
  71.  
  72.             IO.File.Delete(Me.whereToSave)
  73.  
  74.             Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
  75.  
  76.             Me.Invoke(cancelDelegate, True)
  77.  
  78.             Exit Sub
  79.  
  80.         End If
  81.  
  82.         Dim completeDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
  83.         Try
  84.             Me.Invoke(completeDelegate, False)
  85.         Catch ex As Exception
  86.             MsgBox(ex.Message)
  87.         End Try
But im pretty sure my problem is in this part of the code:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  Do
  3.  If BackgroundWorker1.CancellationPending Then 'If user abort download
  4.                 Exit Do
  5.             End If
  6.  
  7.             speedtimer.Start()
  8.  
  9.             Dim readBytes(4095) As Byte
  10.             Dim bytesread As Integer = theResponse.GetResponseStream.Read(readBytes, 0, 4096)
  11.  
  12.             nRead += bytesread
  13.             Dim percent As Integer = (nRead / length) * 100
  14.  
  15.             Me.Invoke(safedelegate, length, nRead, percent, currentspeed)
  16.  
  17.             If bytesread = 0 Then Exit Do
  18.  
  19.             writeStream.Write(readBytes, 0, bytesread)
  20.  
  21.             speedtimer.Stop()
  22.  
  23.             readings += 1
  24.             If readings >= 5 Then 'For increase precision, the speed it's calculated only every five cicles
  25.                 currentspeed = 20480 / (speedtimer.ElapsedMilliseconds / 1000)
  26.                 speedtimer.Reset()
  27.                 readings = 0
  28.             End If
  29.         Loop



joedeene's Avatar
Site Addict
 
Join Date: Jul 2008
Location: US of A
Posts: 587
#2: Nov 9 '08

re: VB.NET-APP Downloading file using httpwebrequest/response


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:

Expand|Select|Wrap|Line Numbers
  1.       Try
  2.         'All Code
  3.         Catch ex As Exception
  4.             MessageBox.Show(ex.ToString())
  5.         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
Member
 
Join Date: Nov 2007
Location: Alberta, Canada
Posts: 59
#3: Nov 9 '08

re: VB.NET-APP Downloading file using httpwebrequest/response


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.
joedeene's Avatar
Site Addict
 
Join Date: Jul 2008
Location: US of A
Posts: 587
#4: Nov 10 '08

re: VB.NET-APP Downloading file using httpwebrequest/response


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.Network.DownloadFile() Method, it's kind of the lazy way, but if it works?

joedeene
Member
 
Join Date: Nov 2007
Location: Alberta, Canada
Posts: 59
#5: Nov 13 '08

re: VB.NET-APP Downloading file using httpwebrequest/response


because i want to get the speed and progress of the download
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#6: Nov 13 '08

re: VB.NET-APP Downloading file using httpwebrequest/response


Hmm these lines:
theRequest.Timeout = 30000
theRequest.KeepAlive = 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?
Member
 
Join Date: Nov 2007
Location: Alberta, Canada
Posts: 59
#7: Dec 2 '08

re: VB.NET-APP Downloading file using httpwebrequest/response


its usually just a random point that it just decides to stop downloading, i did find a quick fix though... instead of using a backgroundworker i just put it all in a different sub and called application.doevents, and this seemed to fix it.
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#8: Dec 2 '08

re: VB.NET-APP Downloading file using httpwebrequest/response


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)
Reply


Similar .NET Framework bytes