Search code examples
ssldelphi-7indyidhttp

Read and save part of file stream via IdHTTP and IdSSLIOHandlerSocketOpenSSL1


I want to download a part of a file from a HTTPS server. I have used an Idhttp and an IdSSLIOHandlerSocketOpenSSL-component. Using the TIdHTTP.Response.AcceptRanges property does not seem to work: it downloads the entire file.

If I use IdHTTP1.Head() and IdHTTP1.Response.AcceptRanges I get nothing back: an empty string.

What am I doing wrong?


Solution

  • The TIdHTTP.Response.AcceptRanges property tells you whether the server accepts byte ranges for a given URL, after you have requested the URL. You have to use the TIdHTTP.Request.Range(s) property to actually specify a range when requesting the URL. You can use TIdHTTP.Head() to check the AcceptRanges value before then requesting the actual URL data using TIdHTTP.Get() with or without a byte range accordingly.

    However, if the AcceptRanges propery is blank after calling TIdHTTP.Head(), then the server simply does not support byte ranges for that URL, so you have no choice but to download the URL data in its entirety, and just discard whatever portions you don't want from it. You could use a TIdEventStream for that, or a custom derived TStream class that overrides the virtual TStream.Write() method. Either approach would allow you to ignore data being downloaded, just keep track of how many bytes are being "written" to your stream, ignoring bytes until you reach the desired starting offset, and then save the remaining data as needed until the desired ending offset is reached.