HTTP download issue

I have the following code running in loop. The loop increments numbers that are included in the file name.

If I un-comment the MsgBox line, and dismiss the message after every download, all of the files download properly.

If I comment it out, all of the downloaded files are created, but are 0K (empty) except for the last one.

HTTPSocket last error code is “0.”

I also have MsgBox " Error " + str(code) + " state encountered." in the Error event of the socket, but it never generates a message,

If currentDownloadFile <> nil then
// Asynchronous.
Socket_Clone.get(theURL, currentDownloadFile)
MsgBox Str(Socket_Clone.LastErrorCode)
End If

What am I missing?

It looks like this is not possible without creating an array and using pop or downloading synchronously.

You are using the asynchronous version of that command, meaning that it returns to your code immediately as it downloads in the background. That would be fine except that you immediately tell it to download the next file, canceling the first.

If you want them all to try to download simultaneously in the background, use an array. Otherwise, provide a timeout so it finishes one download before moving to the next.

If currentDownloadFile <> nil then
  // Synchronous.
  Socket_Clone.get(theURL, currentDownloadFile, 10)
  MsgBox Str(Socket_Clone.LastErrorCode)
End If

Or, do it synchronously.

Or, keep it asynchronous, but have the next file start downloading after the first has completed by putting more logic into the DownloadComplete that walks through an array of files to download.

Depending on the number of items you need to download, doing them asynchronously but sequentially is likely the best way to go about it. Otherwise you may hammer the server and ultimately slow ALL the transfers.