Use BeginStreamed when a request or response is too large to hold in memory. This example sends a file with PUT and writes the successful response into a second stream. Set the client's host, port and trusted roots as in Download a page over HTTPS.
ServerName to the name in the certificate - see Download a page over HTTPS.uses
System.Classes, System.SysUtils, StreamSec.Mobile.HTTPSClient;
function StreamTransfer(aClient: TsmCustomHTTPSClient; const aURL: string; aSource, aDestination: TStream; aMaxResponseBytes: Int64; aWaitMS: Cardinal): Integer; var lExchange: iHttpStreamedExchange; begin lExchange := aClient.BeginStreamed('PUT', aURL, nil, aSource, False, aDestination, False, aMaxResponseBytes); try if not lExchange.WaitForEnd(aWaitMS) then begin if lExchange.Failed then raise eHTTPSClientError.Create(lExchange.FailText); lExchange.Cancel; raise eHTTPSClientError.Create('Timed out waiting for the transfer'); end; Result := lExchange.Response.StatusCode; finally lExchange.DiscardBody; end; end;
StreamTransfer with the URL, both streams, the maximum response size in bytes and the time in milliseconds to wait after submission. The two False arguments keep ownership with the caller. Keep the streams open and leave their positions alone until the function returns or raises, then release each in a finally block.Pass the input path and a separate temporary output path. The function opens the output with fmCreate, which truncates an existing file. Keep any final output at a different path until the transfer succeeds.
function TransferFile(aClient: TsmCustomHTTPSClient; const aURL, aInputFile, aTemporaryOutput: string; aMaxResponseBytes: Int64; aWaitMS: Cardinal): Integer; var lSource, lDestination: TFileStream; begin lSource := TFileStream.Create(aInputFile, fmOpenRead or fmShareDenyWrite); try lDestination := TFileStream.Create(aTemporaryOutput, fmCreate); try Result := StreamTransfer(aClient, aURL, lSource, lDestination, aMaxResponseBytes, aWaitMS); finally lDestination.Free; end; finally lSource.Free; end; end;
A 2xx response goes to the destination. The byte limit you pass to BeginStreamed applies even if the server omits Content-Length. Choose a limit that fits the destination. Zero permits a response of any size.
For a redirect or an HTTP error, the function returns the status code, leaves the destination untouched and discards the response body. The client does not follow a redirect or replay the input stream. A failed transfer can leave a partial file. A timeout in this example cancels the exchange.
If the input length is unknown, use the producer interface on BeginUpload. The HTTPS client notes describe its chunk limits and how to retry a chunk when the upload queue is full.
TsmHTTPSClient · StreamSec.Mobile.HTTPSClient