HttpWebResponse doesnt throws error when network not available

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Geniusg
    New Member
    • Jul 2009
    • 3

    #1

    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
  • Geniusg
    New Member
    • Jul 2009
    • 3

    #2
    code snippet

    Code:
    do
    {
          bytesSize = Response.Read(downBuffer, 0, downBuffer.Length);
          if (bytesSize > 0)
          {
                Local.Write(downBuffer, 0, bytesSize);
                if(!Response.CanRead)
                    break;
                if (goPause == true)
            	break;
         }
    }
    while (bytesSize > 0);
    If there is no network, the flow hangs on line# 3. I expect an exception.

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      I am assuming your Response object is of type NetworkStream? From the .GetResponseStr eam() 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

      Comment

      • Geniusg
        New Member
        • Jul 2009
        • 3

        #4
        Hi Plater..
        Thanks for replying..
        The Response object is of type System.IO.Strea m. HttpWebResponse .GetResponseStr eam 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

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          HttpWebResponse .GetResponseStr eam returns a stream yes, but the actual data type is NetworkStream. You can type cast it.

          Comment

          Working...