473,387 Members | 1,582 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,387 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 2087

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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.