Home All Groups Group Topic Archive Search About

Dowloading through webrequest class

Author
19 Apr 2006 7:40 PM
Praveen
Hi all,
I have written a class to download pages and files from the internet. The
code is like below

   if (WebResp.StatusCode != HttpStatusCode.OK)
   { // throw error }

    Stream Strm = WebResp.GetResponseStream();
    int n = Strm.Read(b,0,b.Length);
    while(n > 0)
    {
         FS.Write(b,0,n);
         n = Strm.Read(b,0,b.Length);
    }

WebResp is the webresponse object from which I get to the download stream. I
am not using the content length to find the file size and do the loop for
that length because http response may not always give the content length.
This works perfectly well except a case if the internet connection breaks
while the code is in the while loop. In this case the Strm.read returns -1
insted of throwing and error that the stream is broken. What happens is the
file is partially downloaded and no error thrown so the application believes
the file download is complete.

Can you suggest some ways of throwing an error if the connection has broken.

Thanks in advance,
Praveen.

Author
19 Apr 2006 8:52 PM
AMercer
I use ReadBlock() instead of Read(), and I get an exception if the connection
is broken.

Show quote
>     Stream Strm = WebResp.GetResponseStream();
>     int n = Strm.Read(b,0,b.Length);
>     while(n > 0)
>     {
>          FS.Write(b,0,n);
>          n = Strm.Read(b,0,b.Length);
>     }
> WebResp is the webresponse object from which I get to the download stream. I
> am not using the content length to find the file size and do the loop for
> that length because http response may not always give the content length.
> This works perfectly well except a case if the internet connection breaks
> while the code is in the while loop. In this case the Strm.read returns -1
> insted of throwing and error that the stream is broken. What happens is the
> file is partially downloaded and no error thrown so the application believes
> the file download is complete.
Author
20 Apr 2006 6:12 AM
Steven Cheng[MSFT]
Hi Praveen,

As for the HttpWebResponse's returned Stream object, it is of an internal
type and the exception handling logic is also encapsulated internally. So
far we haven't any means to explicitly control it. However, as for .NET's
Stream object, the read method will always return non-Zero bytes before
arrive the end of the stream, and -1 is certianly an unexpected value. So I
think you can detect the error depend on this return value, any value less
than 0 should indicate an problem with the internal connection. And when
some serious problem occurs, I'm sure exception will be thrown.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Author
26 Apr 2006 2:36 PM
Praveen
Hello Steven,
Sorry for a late response on this. Actually I was wrong on my previous
observation. if the connection is broken the stream read function returns 0
as against -1 which I mentioned earlier.
I am testing the code with the url pointing to a huge file on my local
machine. during the download process I shut down the IIS.

Surprisingly when I am debugging with break points the read function gives
an error but when there is not break point the read function of the stream
returns 0 and throws no error.

can you suggest some workaround.
thanks in advance,

Praveen.

Show quote
"Steven Cheng[MSFT]" <stch***@online.microsoft.com> wrote in message
news:5R5N9FEZGHA.4224@TK2MSFTNGXA01.phx.gbl...
> Hi Praveen,
>
> As for the HttpWebResponse's returned Stream object, it is of an internal
> type and the exception handling logic is also encapsulated internally. So
> far we haven't any means to explicitly control it. However, as for .NET's
> Stream object, the read method will always return non-Zero bytes before
> arrive the end of the stream, and -1 is certianly an unexpected value. So
> I
> think you can detect the error depend on this return value, any value less
> than 0 should indicate an problem with the internal connection. And when
> some serious problem occurs, I'm sure exception will be thrown.
>
> Regards,
>
> Steven Cheng
> Microsoft Online Community Support
>
>
> ==================================================
>
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
>
> ==================================================
>
>
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>
>
>
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
>
Author
27 Apr 2006 10:02 AM
Steven Cheng[MSFT]
Thanks for your response Praveen,

So the return value from Read in error condition is also 0, this really
make it difficult to troubleshoot. BTW, have you tried use an explicit  
try....catch.... block around the ResponseStream's read block to see
whether it will capture any exception when the connection get broken?  I
still feel a bit strange that it does nothing when there occurs problem
with the connection. Also, does it behaves same under debug and release
mode?

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

AddThis Social Bookmark Button