Xbase++
Xbase++
Client Certificate in REST (USB Token or Smartcard)
See more REST Examples
Demonstrates how to use a client certificate with a REST connection where the certificate and private key are located on a USB token or smart card.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oRest
LOCAL oSocket
LOCAL oCert
LOCAL nBTls
LOCAL nPort
LOCAL nMaxWaitMs
LOCAL nBAutoReconnect
LOCAL cResponseJson
nSuccess := 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// This example shows how to use the Chilkat socket object's connection.
oRest := CreateObject("Chilkat.Rest")
oSocket := CreateObject("Chilkat.Socket")
// Set the certificate to be used for mutual TLS authentication
oCert := CreateObject("Chilkat.Cert")
// If the smartcard or token requires a PIN...
oCert:SmartCardPin := "000000"
nSuccess := oCert:LoadFromSmartcard("")
IF (nSuccess != 1)
? oCert:LastErrorText
oRest:destroy()
oSocket:destroy()
oCert:destroy()
RETURN
ENDIF
nSuccess := oSocket:SetSslClientCert(oCert)
IF (nSuccess != 1)
? oSocket:LastErrorText
oRest:destroy()
oSocket:destroy()
oCert:destroy()
RETURN
ENDIF
// Establish the connection using the socket object (with client certificate authentication).
nBTls := 1
nPort := 443
nMaxWaitMs := 5000
nSuccess := oSocket:Connect("www.example.com", nPort, nBTls, nMaxWaitMs)
IF (nSuccess != 1)
? "Connect Failure Error Code: " + Str(oSocket:ConnectFailReason)
? oSocket:LastErrorText
oRest:destroy()
oSocket:destroy()
oCert:destroy()
RETURN
ENDIF
nBAutoReconnect := 1
// Use the connection:
nSuccess := oRest:UseConnection(oSocket, nBAutoReconnect)
IF (nSuccess != 1)
? oRest:LastErrorText
oRest:destroy()
oSocket:destroy()
oCert:destroy()
RETURN
ENDIF
// At this point we are connected and can make REST calls...
// For example..
cResponseJson := oRest:FullRequestNoBody("GET", "/someQuery")
IF (oRest:LastMethodSuccess != 1)
? oRest:LastErrorText
oRest:destroy()
oSocket:destroy()
oCert:destroy()
RETURN
ENDIF
oRest:destroy()
oSocket:destroy()
oCert:destroy()