VB.NET-APP Downloading file using httpwebrequest/response | Member | | Join Date: Nov 2007 Location: Alberta, Canada
Posts: 59
| |
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: - '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: -
-
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
|  | Site Addict | | Join Date: Jul 2008 Location: US of A
Posts: 587
| | | 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: - 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
| | Member | | Join Date: Nov 2007 Location: Alberta, Canada
Posts: 59
| | | 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.
|  | Site Addict | | Join Date: Jul 2008 Location: US of A
Posts: 587
| | | 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
| | | re: VB.NET-APP Downloading file using httpwebrequest/response
because i want to get the speed and progress of the download
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,161
| | | 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
| | | 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.
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,161
| | | 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)
|  | Similar .NET Framework bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,414 network members.
|