Dowloading through webrequest class

  • Thread starter Thread starter Praveen
  • Start date Start date
P

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.
 
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.)
 
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.
 
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.)
 
Back
Top