473,385 Members | 1,927 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2086

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chris P. | last post by:
I have a C# application that connects to Perl application on a UNIX server. I am able to connect and communicate both directions with the server, but there are several occasions when it appears...
14
by: Laszlo Szijarto | last post by:
Can BinaryReader be forced to read a stream, say a TCP/IP stream or memory stream or even file stream in big endian order or do I have to write something custom to reverse the byte order? So, for...
4
by: Robert Misiak | last post by:
I've been trying out the .NET Memory Profiler (http://www.scitech.se/memprofiler/) and I found an excessive number of undisposed instances of BinaryReader after opening and then closing various...
6
by: Question with BinaryReader | last post by:
I use BinaryReader to read my binary dafa files, when i call ReadBytes, why it always return more 4 bytes. The following is my code. FileStream fs = new FileStream(file, FileMode.OpenOrCreate,...
1
by: Vitaly | last post by:
// Open input file and create the BinaryReader. br = new BinaryReader(new FileStream("Test.dat", FileMode.Open, FileAccess.Read)); // Read binary data. d = br.ReadDouble(); A question is...
0
by: Denny Rue | last post by:
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...
2
by: Nuno Magalhaes | last post by:
In pages that there is no content length, how does HttpWebResponse knows where the page ends? And what kind of objects/methods does it retrieve? Does it only retrieve the initial page without any...
2
by: Brad Wood | last post by:
<environment> Consuming a web service from a VS2005 beta 2 test harness on WinXP. Web service built with VS2003 running on my machine with IIS 5X set to use ASP.NET 1.1 </environment> I have...
3
by: =?Utf-8?B?VmljdG9y?= | last post by:
Hi, Could you tell me can I keep the MemoryStream open and "close" the BinaryReader? As the MemoryStream is used for buffering the TCP data and BinaryReader is only used to read the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.