468,766 Members | 1,295 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,766 developers. It's quick & easy.

Time Out for Binaryreader from HTTPWebResponse

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

Jul 21 '05 #1
0 1835

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Question with BinaryReader | last post: by
2 posts views Thread by Nuno Magalhaes | last post: by
2 posts views Thread by Brad Wood | last post: by
3 posts views Thread by =?Utf-8?B?VmljdG9y?= | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.