Connecting Tech Pros Worldwide Forums | Help | Site Map

HttpWebResponse doesnt throws error when network not available

Newbie
 
Join Date: Jul 2009
Posts: 3
#1: Jul 9 '09
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

Newbie
 
Join Date: Jul 2009
Posts: 3
#2: Jul 9 '09

re: HttpWebResponse doesnt throws error when network not available


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.
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#3: Jul 9 '09

re: HttpWebResponse doesnt throws error when network not available


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
Newbie
 
Join Date: Jul 2009
Posts: 3
#4: Jul 10 '09

re: HttpWebResponse doesnt throws error when network not available


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
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#5: Jul 10 '09

re: HttpWebResponse doesnt throws error when network not available


HttpWebResponse.GetResponseStream returns a stream yes, but the actual data type is NetworkStream. You can type cast it.
Reply