473,320 Members | 1,990 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,320 software developers and data experts.

HttpWebResponse doesnt throws error when network not available

I am using a HttpWebResponse object to download a file from remote location. The problem is that while going through a loop to download the file, if the network goes off, then the code doesnt throws any exception.
I checked in .Net framework 1.0 and 1.1 and the same problem persists. Though in 2.0, i get an IOException.
Can anyone throw some light on this? i am not sure if there is anything fishy about my code or is this a problem with the frameworks 1.0 and 1.1

Thanks !!
- Gaurav
Jul 9 '09 #1
4 1823
Expand|Select|Wrap|Line Numbers
  1. do
  2. {
  3.       bytesSize = Response.Read(downBuffer, 0, downBuffer.Length);
  4.       if (bytesSize > 0)
  5.       {
  6.             Local.Write(downBuffer, 0, bytesSize);
  7.             if(!Response.CanRead)
  8.                 break;
  9.             if (goPause == true)
  10.             break;
  11.      }
  12. }
  13. while (bytesSize > 0);
  14.  
If there is no network, the flow hangs on line# 3. I expect an exception.
Jul 9 '09 #2
Plater
7,872 Expert 4TB
I am assuming your Response object is of type NetworkStream? From the .GetResponseStream() function on the HttpWebResponse object?

I am guessing it has something to do with the timeout. I bet the underlying exception to the IOException is that there was a teimout waiting to read. The timeout wasn't added until .NET2.0
You could try checking the DataAvailable property before reading to make surethere is still data there to read
Jul 9 '09 #3
Hi Plater..
Thanks for replying..
The Response object is of type System.IO.Stream. HttpWebResponse.GetResponseStream returns a type of Stream object, which doesnt has DataAvailable property :-(
I have found a workaround for this problem. Inside the loop, I check for bytesSize and if it = -1, i throw an IOException (assuming that network is down). I am testing this piece of code by pulling out the network cable from my system when the while-loop is running. Though this workaround seems to work for now, i am not sure if it a good thing to do !!
Any suggestions from your side?

Thanks !!
- Gaurav
Jul 10 '09 #4
Plater
7,872 Expert 4TB
HttpWebResponse.GetResponseStream returns a stream yes, but the actual data type is NetworkStream. You can type cast it.
Jul 10 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Mark Vuksani | last post by:
Hi, I have a trigger like this (simplified to illustrate the problem): -------- CREATE TRIGGER Test ON . FOR INSERT, UPDATE, DELETE AS RAISERROR( 'test trigger error !', 16, 1)
2
by: Daniel | last post by:
if System.IO.StreamWriter write throws an exception, is there anyway to close the System.IO.StreamWriter object? it seems to stay open when this happens then future attempts to write to that same...
10
by: Brian Conway | last post by:
I have no idea what is going on. I have a Login screen where someone types in their login information and this populates a datagrid based off of the login. Works great in debug and test through...
13
by: Jason Manfield | last post by:
For some URLs (e.g.http://v3.espacenet.com/origdoc?DB=EPODOC&IDX=WO2005028634&F=0&QPN=WO2005028634), the content length for the HttpWebResponse I get with request.GetResponse in empty. The...
1
by: Daniel | last post by:
If my System.IO.StreamWriter Write method throws "The specified network name is no longer available." and I try to Dispose or Close it in the finaly clause the close or dispose method just throws...
4
by: Qingdong Z. | last post by:
I used HttpWebRequest/HttpWebResponse to get the content of an URL. It throws "HTTP Protocol Violation" sometimes. And I set useUnsafeHeaderParsing = "true" to fix the problem in most case, but for...
0
by: Sadiq | last post by:
Hi, I am making a proxy in VB.NET. I have problem in recieving the response from the server in HTTPWebResponse Class. It gives me errors like 1. "the server returned error 404 not found" ....on...
1
by: stewart.fay | last post by:
Dear all, I am using the HttpWebRequest and HttpWebRequest to send an XML request to an ASPX page. When I use the browser and add my XML request the request is posted fine and an answer is...
0
by: nukiee | last post by:
I wanted to check the status code that comes back as response from the HttpWebResponse. So I used the following code - res = (HttpWebResponse) req.GetResponse(); It's working great, but if I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.