TsgcHTTP1Client is a non-visual component that inherits from TIdHTTP indy component and adds some new properties.
This component is located in sgcHTTP unit.
Allows to configure how connect to secure SSL/TLS servers using HTTP/1 protocol
ALPNProtocols: list of the ALPN protocols which will be sent to server.
RootCertFile: path to root certificate file.
CertFile: path to certificate file.
KeyFile: path to certificate key file.
Password: if certificate is secured with a password, set here.
VerifyCertificate: if certificate must be verified, enable this property. Use the event OnSSLVerifyPeer to customize the SSL verification.
VerifyDepth: is an Integer property that represents the maximum number of links permitted when verification is performed for the X.509 certificate.
Version: by default uses TLS 1.0, if server requires a higher TLS version, here can be selected.
Proxy: here you can define if you want to connect through a Proxy Server, you can connect to the following proxy servers:
pxyHTTP: HTTP Proxy Server.
pxySocks4: SOCKS4 Proxy Server.
pxySocks4A: SOCKS4A Proxy Server.
pxySocks5: SOCKS5 Proxy Server.
IOHandler: select which library you will use to connection using TLS.
iohOpenSSL: uses OpenSSL library and is the default for Indy components. Requires to deploy openssl libraries for win32/win64.
iohSChannel: uses Secure Channel which is a security protocol implemented by Microsoft for Windows, doesn't require to deploy openssl libraries. Only works in Windows 32/64 bits.
OpenSSL_Options: configuration of the openSSL libraries.
APIVersion: allows defining which OpenSSL API will be used.
oslAPI_1_0: uses API 1.0 OpenSSL, it's latest supported by Indy
oslAPI_1_1: uses API 1.1 OpenSSL, requires our custom Indy library and allows using OpenSSL 1.1.1 libraries (with TLS 1.3 support).
oslAPI_3_0: uses API 3.0 OpenSSL, requires our custom Indy library and allows using OpenSSL 3.0.0 libraries (with TLS 1.3 support).
LibPath: here you can configure where are located the openSSL libraries
oslpNone: this is the default, the openSSL libraries should be in the same folder where is the binary or in a known path.
oslpDefaultFolder: sets automatically the openSSL path where the libraries should be located for all IDE personalities.
oslpCustomFolder: if this is the option selected, define the full path in the property LibPathCustom.
LibPathCustom: when LibPath = oslpCustomFolder define here the full path where are located the openSSL libraries.
UnixSymLinks: enable or disable the loading of SymLinks under Unix systems (by default is enabled, except under OSX64):
oslsSymLinksDefault: by default are enabled except under OSX64 (after MacOS Monterey fails trying to load the library without version.).
oslsSymLinksLoadFirst: Load SymLinks and do before trying to load the version libraries.
oslsSymLinksLoad: Load SymLinks after trying to load the version libraries.
oslsSymLinksDontLoad: don't load the SymLinks.
MinVersion: set here the minimum version that will use the client to connect to a secure server. By default, the value is tlsUndefined which means the minimum version is the same which has been set in the Version property. Example: if you want to set the Client to only connect using TLS 1.2 or TLS 1.3 set the following values.
SSLOptions.Version := tls1_3;
SSLOptions.OpenSSL_Options.MinVersion := tls1_2;
SChannel_Options: allows to use a certificate from Windows Certificate Store.
CertHash: is the certificate Hash. You can find the certificate Hash running a dir command in powershell.
CipherList: here you can set which Ciphers will be used (separated by ":"). Example: CALG_AES_256:CALG_AES_128
CertStoreName: the store name where is stored the certificate. Select one of below:
scsnMY (the default)
scsnCA
scsnRoot
scsnTrust
CertStorePath: the store path where is stored the certificate. Select one of below:
scspStoreCurrentUser (the default)
scspStoreLocalMachine
If Log property is enabled it saves socket messages to a specified log file, useful for debugging.
LogOptions.FileName: full path to the filename.
Allows to Authenticate using OAuth2 or JWT.
By default the HTTP1Client uses blocking requests, so after calling an HTTP Request method, the client waits the response from the server. Alternatively, 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.
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.
void __fastcall OnAsyncResultEvent(TObject* Sender, const TsgcHTTPAsyncRequest* aRequest, const TIdHTTPResponse* aResponse)
If there is any error while processing the Asynchronous request, the exception will be raised in the event OnAsyncException.
Request a GET method to HTTPs server and using TLS 1.2
TsgcHTTP1Client *oHTTP = new TsgcHTTP1Client()
try
{
oHTTP->TLSOptions->Version = tls1_2;
ShowMessage(oHTTP->Get("https://www.google.es"));
}
__finally
}
oHTTP->Free();
}
Request a GET method to HTTPs server using openSSL 1.1 and TLS 1.3
TsgcHTTP1Client *oHTTP = new TsgcHTTP1Client()
try
{
oHTTP->TLSOptions->OpenSSL_Options->APIVersion = oslAPI_1_1;
oHTTP->TLSOptions->Version = tls1_2;
ShowMessage(oHTTP->Get("https://www.google.es"));
}
__finally
}
oHTTP->Free();
}
Request an Asynchronous POST method and read the response using the OnAsyncResultEvent.
void __fastcall OnAsyncExceptionEvent(TObject* Sender, const TsgcThread* aThread, const Exception* E)
{
Log(E->Message);
}
void __fastcall OnAsyncResultEvent(TObject* Sender, const TsgcHTTPAsyncRequest* aRequest, const TIdHTTPResponse* aResponse)
{
if (aResponse->ResponseCode == 200)
Log("ok", aRequest->Response);
else
Log("error", aRequest->Response);
}
TsgcHTTP1Client* oHTTP = new TsgcHTTP1Client(NULL);
oHTTP->OnAsyncResult = OnAsyncResultEvent;
oHTTP->OnAsyncException = OnAsyncExceptionEvent;
TStringStream* oRequest = new TStringStream("body");
TStringStream* oResponse = new TStringStream("");
oHTTP->PostAsync("https://localhost/test", oRequest, oResponse);
Request a GET method to HTTPs server using SChannel for Windows.
TsgcHTTP1Client *oHTTP = new TsgcHTTP1Client()
try
{
oHTTP->TLSOptions->IOHandler = iohSChannel;
oHTTP->TLSOptions->Version = tls1_2;
ShowMessage(oHTTP->Get("https://www.google.es"));
}
__finally
}
oHTTP->Free();
}
Request SSE method to get data events
TsgcHTTP1Client *oHTTP = new TsgcHTTP1Client();
oHTTP->OnSSEMessage() = OnSSEMessageEvent();
oHTTP->GetSSE("https://www.yoursite.com/sse");
void OnSSEMessageEvent(TObject *Sender, const string aMessage, ref bool Cancel)
{
ShowMessage(aMessage);
}
OnSSEMessage
The event is called when a new SSE message is received.
OnSSLVerifyPeer
If verify certificate is enabled, in this event you can verify and decide whether to accept the server certificate.
OnSSLGetHandler
This event is raised before SSL handler is created, you can create here your own SSL Handler (needs to be inherited from TIdServerIOHandlerSSLBase or TIdIOHandlerSSLBase) and set the properties needed
OnSSLAfterCreateHandler
If no custom SSL object has been created, it creates by default using OpenSSL handler. You can access to SSL Handler properties and modify if needed
OnAsyncResult
The event is called after requesting an Async method (using GetAsync, PutAsync... methods). Use the Response parameter to know the result of the request.
OnAsyncException
If there is any error while processing an async request, this event is called with the exception raised.