Time Out for Binaryreader from HTTPWebResponse

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I’ve created VB code to download files from a web site through the use of
HTTPWebRequest, HTTPWebResponse and BinaryReader.

The HTTPWebRequest has a TimeOut property to limit how long it waits for a
corresponding HTTPWebResponse. This works just fine. However, the timeout
does not appear to apply to a BinaryReader created from the HTTPWebResponse.
I’ve had instances where the procedures will hang indefinitely during the
download.

Is there a means to force a “time out†on the BinaryReader?

CODE EXAMPLE:
In this example, parameters are posted to the web site and a file retrieved.
No proxy is involved.

Dim myHTTPWebRequest as HTTPWebRequest
Dim myHTTPWebResponse as HTTPWebResponse
Dim sbPostData as new Stringbuilder
Dim sbWebAddress as new stringbuilder
Dim myTextWriter As TextWriter
Dim myBinaryReader As BinaryReader
Dim myBinaryWriter as BinaryWriter
Dim intBuffer As Integer = 65536
Dim byteBuffer() As Byte


Const _cnstHTTP_Accept_All As String = "image/gif, image/x-xbitmap,
image/jpeg, image/pjpeg, application/vnd.ms-excel,
application/vnd.ms-powerpoint, application/msword,
application/x-shockwave-flash, */*"
Const _cnstHTTP_ContentType_URLEncoded As String =
"application/x-www-form-urlencoded"
Const _cnstHTTP_Method_Post As String = "POST"
Const _cnstHTTP_UserAgent As String = "Mozilla/4.0 (compatible; MSIE 6.0;
Windows NT 5.1; .NET CLR 1.1.4322)"

‘ Create HTTPWebRequest
‘
With sbWebAddress
.remove(0, .Length)
.append(www.bogusaddress.com/download)
End

‘ Set post data
‘
With sbPostData
.remove(0, .length)
.append(“Begin=12/1/2004â€)
.append(“&â€)
.append(“EndDate=12/2/2004â€)
End with

‘ Create web request
‘
myHTTPWebRequest = CType(HttpWebRequest.Create(sbWebAddress.ToString),
HttpWebRequest)

' Set parms
‘
With myHTTPWebRequest
..Accept = _cnstHTTP_Accept_All
..ContentLength = sbPostData.Length
..ContentType = _cnstHTTP_ContentType_URLEncoded
..KeepAlive = True
..Method = _cnstHTTP_Method_Post
..Referer = "http://www.bogusaddress.com/referer"
..Timeout = 60000
..UserAgent = _cnstHTTP_UserAgent

..Headers.Add("Accept-Encoding", "gzip, deflate")
..Headers.Add("Accept-Language", "en-us")
..Headers.Add("Cache-Control", "no-cache")
End With

' Send parms
'
myTextWriter = CType(New StreamWriter(myHTTPWebRequest.GetRequestStream),
TextWriter)
myTextWriter.Write(sbPostData.ToString)
myTextWriter.Close()

myHTTPWebResponse = CType(myHTTPWebRequest.GetResponse, HttpWebResponse)

‘ Get file stream
'
myBinaryReader = New BinaryReader(myHTTPWebResponse.GetResponseStream)
myBinaryWriter = New BinaryWriter(New FileStream(“c:\temp\bogus.txtâ€,
FileMode.Create))

' Read/write
'
byteBuffer = myBinaryReader.ReadBytes(intBuffer)

‘ As best as I can tell, this is where it hangs
‘
Do While byteBuffer.Length > 0
myBinaryWriter.Write(byteBuffer)
myBinaryWriter.Flush()
byteBuffer = myBinaryReader.ReadBytes(intBuffer)
Loop

myBinaryWriter.Close
myHTTPWebResponse.Close
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top