|
-
May 26th, 2010, 02:21 PM
#1
Thread Starter
Fanatic Member
Webrequest timed out second time
Hello.
I finally sorted out what my problem was with downloading files.
Now I need some help to solve this. 
Description:
- In my code a webrequest is created and the response is read. The data is converted into a stream which it then saves to a file.
- When I start the download for the first time, no problems occur. But the second time I start the download with the same URL it times out at the GetResponse() part. The webexception I get is: "The operation has timed out".
- dledsize is of type long and represents the downloaded amount of bytes
- URL represents the URL to the download
- The DestFileName of type string represents the destination file to save to.
- This problem occurs with any URL I use to download twice.
This is the code of the download sub:
Code:
Me.dledsize = 0
Dim wr As Net.HttpWebRequest = Net.HttpWebRequest.Create(Me.URL)
Debug.WriteLine("Generating response...")
Dim ws As Net.HttpWebResponse = Nothing
ws = wr.GetResponse
Debug.WriteLine("Response created")
Dim str As IO.Stream = ws.GetResponseStream()
Dim inBuf(Me.tldlsize) As Byte
Dim bytesToRead As Integer = CInt(inBuf.Length)
While bytesToRead > 0
If Me.Paused = False Then
Dim n As Integer = str.Read(inBuf, Me.dledsize, bytesToRead)
If n = 0 Then
Exit While
End If
Me.dledsize += n
bytesToRead -= n
End If
End While
Dim fstr As New IO.FileStream(Me.DestFileName, IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
fstr.Write(inBuf, 0, Me.dledsize)
str.Close()
fstr.Close()
wr.Abort()
ws.Close()
Me.Downloading = False
Me.dlfin = True
Anyone knows why a timed out exception is thrown when I download the same url twice? 
EDIT//////
Ok DAMM sorry for bugging you with this.
My code here is fine, the following line was the cause:
tldlsize = Net.WebRequest.Create(Me.URL).GetResponse.ContentLength
Of course the response hasn't closed, and the second time it starts it reached a 3th response.
This causes...the exception.
So, DOH for me! xD
Last edited by bergerkiller; May 26th, 2010 at 03:42 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|