473,654 Members | 3,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 myHTTPWebReques t as HTTPWebRequest
Dim myHTTPWebRespon se 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_Accep t_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_Conte ntType_URLEncod ed As String =
"applicatio n/x-www-form-urlencoded"
Const _cnstHTTP_Metho d_Post As String = "POST"
Const _cnstHTTP_UserA gent 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(“Begi n=12/1/2004”)
.append(“&” )
.append(“EndD ate=12/2/2004”)
End with

‘ Create web request

myHTTPWebReques t = CType(HttpWebRe quest.Create(sb WebAddress.ToSt ring),
HttpWebRequest)

' Set parms

With myHTTPWebReques t
..Accept = _cnstHTTP_Accep t_All
..ContentLength = sbPostData.Leng th
..ContentType = _cnstHTTP_Conte ntType_URLEncod ed
..KeepAlive = True
..Method = _cnstHTTP_Metho d_Post
..Referer = "http://www.bogusaddres s.com/referer"
..Timeout = 60000
..UserAgent = _cnstHTTP_UserA gent

..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(my HTTPWebRequest. GetRequestStrea m),
TextWriter)
myTextWriter.Wr ite(sbPostData. ToString)
myTextWriter.Cl ose()

myHTTPWebRespon se = CType(myHTTPWeb Request.GetResp onse, HttpWebResponse )

‘ Get file stream
'
myBinaryReader = New BinaryReader(my HTTPWebResponse .GetResponseStr eam)
myBinaryWriter = New BinaryWriter(Ne w FileStream(“c :\temp\bogus.tx t”,
FileMode.Create ))

' Read/write
'
byteBuffer = myBinaryReader. ReadBytes(intBu ffer)

‘ As best as I can tell, this is where it hangs

Do While byteBuffer.Leng th > 0
myBinaryWriter. Write(byteBuffe r)
myBinaryWriter. Flush()
byteBuffer = myBinaryReader. ReadBytes(intBu ffer)
Loop

myBinaryWriter. Close
myHTTPWebRespon se.Close

Jul 21 '05 #1
0 2106

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

Similar topics

2
2432
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 that the BinaryReader only reads part of the message sent by the server. Here is the main flow of my application. 1. Open Socket 2. Read 4 byte integer 3. Read byte array that is the size of the integer in step 2. 4. Put message in collection
14
19034
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 example, I'd like a Windows client PC (native little endian) to be able to read a network stream coming in as big endian. I want to be able to read a short for example without worrying about flipping the bytes. Thank you, Laszlo
4
1853
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 forms. The thing that these forms have in common is that they are localized for both English and Dutch. I tried adding resources.ReleaseAllResources() from the destructor but that had no effect. I'm pretty sure that it is the localization code...
6
4324
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, FileAccess.Read); BinaryReader br = new BinaryReader(fs); Byte bytes = br.ReadBytes(8); bytes = br.ReadBytes(1); After the first sentence, fs.Position is 0, and all things is ok, but when runs to the second sentence, i found fs.Position is 4,...
1
4794
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 how to do the same but read from standard input stream like Console.In.
0
556
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 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. ...
2
2383
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 images, i.e., does it only makes one "GET / HTTP/1.1\r\n\r\n"? Because I'm doing some projects on a webclient (to measure some statistic times) and the total bytes get byte HttpWebResponse are lower than the total objects I get via socket way. ...
2
2569
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 a web service wherein I make http calls out using the HttpWebResponse.GetResponse method. This works fine when making these calls normally (no threading).
3
4782
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 MemoryStream, so I don't want to close the MemoryStream. Do I really need to close the BinaryReader if the underlying backing store needs not to be closed?
0
8379
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8294
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8494
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8596
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7309
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4297
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2719
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1924
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.