By default the TsgcHTTP1client uses blocking requests, so after calling an HTTP Request method, the client waits the response from the server. From sgcWebSockets 2025.3.0 you can use Asynchronous methods to execute these HTTP Requests in a secondary thread avoiding to block the thread where the requests is called. The following asynchronous methods are implemented:
- GetAsync
- PostAsync
- PutAsync
- OptionsAsync
- DeleteAsync
After calling these methods, instead of waiting the response, the code continue to the next line, and the response can be handled using the event OnAsyncResponse.
procedure OnAsyncResultEvent(Sender: TObject; const aRequest: TsgcHTTPAsyncRequest; const aResponse: TIdHTTPResponse);
If there is any error while processing the Asynchronous request, the exception will be raised in the event OnAsyncException.
Delphi Example
Request an Asynchronous POST method and read the response using the OnAsyncResultEvent.
procedure OnAsyncExceptionEvent(Sender: TObject; const aThread: TsgcThread; const E: Exception); begin Log(E.Message); end; procedure OnAsyncResultEvent(Sender: TObject; const aRequest: TsgcHTTPAsyncRequest; const aResponse: TIdHTTPResponse); begin if aResponse.ResponseCode = 200 then Log('ok', aRequest.Response) else Log('error', aRequest.Response); end; oHTTP := TsgcHTTP1Client.Create(nil); oHTTP.OnAsyncResult := OnAsyncResultEvent; oHTTP.OnAsyncException := OnAsyncResultEvent; oRequest := TStringStream.Create('body'); oResponse := TStringStream.Create(''); oHTTP.PostAsync('https://localhost/test', oRequest, oResponse); // ... the code continues here ...