Home All Groups Group Topic Archive Search About

How to download a file? Need a .NET version of URLDownloadToFile

Author
10 Oct 2005 5:12 AM
John
Hi,
I know, this question was posted few times already,
but I failed to find a definitive answer.

A usual answer is to use System.Net.WebClient.DownloadFile,
but it does not work for me because:
- I need the HTTP request to be identical (or very close)
  to what IE normally sends (this includes cookies and other headers).
  URLDownloadToFile is good at  that, but WebClient is not.

- I need to be notified about progress status, in particular about
  redirection and Mime type. URLDownloadToFile gives a very
  detailed progress and status, but WebClient only gives the
  number of bytes downloaded.

Other people suggest using HttpWebRequest directly, set headers manually,
initialize CookieCollection, etc, etc, etc. But all of this is such a pain!

I wonder whether even the later version of .NET still does not have
a convenient API for file download.

Thank you
John

Author
10 Oct 2005 6:46 AM
Egbert Nierop (MVP for IIS)
Show quote
<John> wrote in message news:85OdnfilwZnJZdTeRVn-tg@speakeasy.net...
> Hi,
> I know, this question was posted few times already,
> but I failed to find a definitive answer.
>
> A usual answer is to use System.Net.WebClient.DownloadFile,
> but it does not work for me because:
> - I need the HTTP request to be identical (or very close)
>  to what IE normally sends (this includes cookies and other headers).
>  URLDownloadToFile is good at  that, but WebClient is not.
>
> - I need to be notified about progress status, in particular about
>  redirection and Mime type. URLDownloadToFile gives a very
>  detailed progress and status, but WebClient only gives the
>  number of bytes downloaded.
>
> Other people suggest using HttpWebRequest directly, set headers manually,
> initialize CookieCollection, etc, etc, etc. But all of this is such a
> pain!
>
> I wonder whether even the later version of .NET still does not have
> a convenient API for file download.

Create DllImport attribute on URLDownloadToFile
and implement the callback function. Let us know if you succeed.
The callback 'pointer' is *especially* meant for what you want.


However, you since can use a binary download on HttpWebRequest and download
in blocks. First you should read the Content-Length
then you can read in blocks of say 4096 bytes use the binary stream
(GetResponseStream()).
Author
12 Oct 2005 4:40 AM
John
> Create DllImport attribute on URLDownloadToFile
> and implement the callback function. Let us know if you succeed.
> The callback 'pointer' is *especially* meant for what you want.

No luck so far.

The last argument of URLDownloadToFile is IBindStatusCallback interface.
I cannot find any type library, which defines IBindStatusCallback,
therefore, I cannot simply import it to C# code.

I guess (but not completely sure) that there must be a way to redeclare
this interface (and several referenced interfaces and data structures)
entirely in C#, but this appears to be such a pain!

I guess the simplest solution would be to write a completely separate
unmanaged DLL, which will perform download.

Any other options?

John

AddThis Social Bookmark Button