Warm tip: This article is reproduced from stackoverflow.com, please click
delphi indy

Is there a way to have my response sent immediately in the TIdHTTPServer.OnCommandGet event?

发布于 2020-04-13 10:44:41

I have some tasks to do for certain commands I receive and would like to keep the client from (read-)timeouts by sending some data while doing the task.

Does the AResponseInfo object offer anything to accomplish this?

I currently use the ContentText property, but it sends the data (as it seems) at the end of the event handler.

Questioner
Wolfgang Bures
Viewed
69
Remy Lebeau 2020-02-04 02:51

By default, TIdHTTPServer sends a response when the OnCommand... event handler exits. However, it is possible to send a response sooner, by either:

  • calling the AResponseInfo.WriteHeader() and AResponseInfo.WriteContent() methods after populating the AResponseInfo properties as needed.

  • writing directly to the TCP socket, and then setting the AResponseInfo.HeaderHasBeenWritten property to true so TIdHTTPServer does not try to send its own response.

Note: once you send a response to the client, the client is free to send new requests using the same TCP connection (if HTTP keep-alives are used), but you will be holding up the connection from processing those requests until you exit the OnCommand... event handler.

If you have long processing to do, it is generally better to send a normal response back to the client as soon as possible, for instance with a 102 Processing response, exit the OnCommand... event handler so the server regains control of the connection, do the actual processing in the background, and let the client query the status of the processing using subsequent requests as needed. You could assign a cookie or other type of server-generated ID to the processing, and then have the client send that cookie/ID back to the server to query the processing's status.