Method ExtInternet.Download(+ 1 overload)
Overload
Downloads content to stream and provides the progress.
public static bool Download(this HttpResponseMessage t, Stream stream, Action<ProgressArgs> progress = null, CancellationToken cancel = default, bool disposeStream = false)
Parameters
| t (HttpResponseMessage) |
|
stream (Stream)
Writes to this stream. |
|
progress (Action<ProgressArgs>)
Calls this callback function to report the progress. If |
|
cancel (CancellationToken)
Can be used to cancel. |
|
disposeStream (bool)
Call |
Returns
|
bool
|
Exceptions
|
HttpRequestException
Failed HTTP request. |
|
Exception
Other exceptions. |
Remarks
By default HttpClient and similar functions download content to a memory buffer before returning. To avoid it, use completionOption System.Net.Http.HttpCompletionOption.ResponseHeadersRead, or ExtInternet.Get with dontWait true. Then call this function (it will download the file), and finally dispose the HttpResponseMessage.
Cannot provide the progress percentage if the content length is unknown. Top reasons:
- The HTTP server uses chunked transfer encoding.
- The HTTP server uses content compression and the HttpClient is configured to automatically decompress (for example internet.http). Instead of internet.http create a HttpClient and optionally set header
"Accept-Encoding: br, gzip, deflate". This function will decompress.
Overload(top)
Downloads content to file and provides the progress.
public static bool Download(this HttpResponseMessage t, string file, Action<ProgressArgs> progress = null, CancellationToken cancel = default)
Parameters
| t (HttpResponseMessage) |
|
file (string)
File path. The function uses pathname.normalize. Creates parent directory if need. |
|
progress (Action<ProgressArgs>)
Calls this callback function to report the progress. If |
|
cancel (CancellationToken)
Can be used to cancel. |
Returns
|
bool
|
Exceptions
|
HttpRequestException
Failed HTTP request. |
|
Exception
Other exceptions. |
Remarks
By default HttpClient and similar functions download content to a memory buffer before returning. To avoid it, use completionOption System.Net.Http.HttpCompletionOption.ResponseHeadersRead, or ExtInternet.Get with dontWait true. Then call this function (it will download the file), and finally dispose the HttpResponseMessage.
Cannot provide the progress percentage if the content length is unknown. Top reasons:
- The HTTP server uses chunked transfer encoding.
- The HTTP server uses content compression and the HttpClient is configured to automatically decompress (for example internet.http). Instead of internet.http create a HttpClient and optionally set header
"Accept-Encoding: br, gzip, deflate". This function will decompress.
Examples
if (!internet.http.Get(url, true).Download(zip)) return;