Page 1 of 1

HttpClient() Chilkat replacement code

Posted: Thu Aug 29, 2024 9:30 am
by jezda
Hello everybody!

Does anyone know a chilkat replacement for this Xbase++ code:

*------------Xbase++ code
LOCAL pcSubjectCN:='ABCD Megasoft'
cUrl='https://api.sandbox.suf.purs.gov.rs/api/v3/sdc/commands'
oHC:=HttpClient():new( cURL )
oHC:setTimeout(,10000,,10000)
oHC:httpRequest:setHeader( "Accept", "application/json" )
oHC:httpRequest:setHeader( "Content-Type", "application/json" )
oHC:httpRequest:setHeader( "TaxCoreAuthenticationToken", pcToken)
oHC:httpRequest:setMethod( "GET" )
oHC:SetCertificate('user\MY\'+alltrim(pcSubjectCN))
cResponse := oHC:send()
nStatus := oHC:getStatusCode()
oHC:disconnect()
*MsgBox(var2char(nStatus))
*MsgBox(var2char(cResponse))

If nStatus=200
*--OK
Else
*--Error
Endif
This works fine on windows 10 but on windows 11 always return error ERROR_WINHTTP_SECURE_FAILURE

I wrote the following code:

*---------------------Chilkat replacement code
LOCAL pcSubjectCN:='ABCD Megasoft'
loRest = CreateObject('Chilkat_9_5_0.Rest')
lnBTls := 1
lnPort := 443
lnBAutoReconnect := 1
lnSuccess := loRest:Connect("https://api.sandbox.suf.purs.gov.rs",lnPort,lnBTls,lnBAutoReconnect)
IF lnSuccess <> 1
msgbox( "ConnectFailReason: " + STR(loRest:ConnectFailReason)+chr(13)+chr(10)+loRest:LastErrorText,'Informacija' )
RELEASE loRest
RELEASE loJson
Return nil
ENDIF
loRest:AddHeader("accept","application/json")
loRest:AddHeader("Content-Type", "application/json")
loRest:AddHeader("TaxCoreAuthenticationToken", pcToken)
loSbRequestBody := CreateObject('Chilkat_9_5_0.StringBuilder')
loSbResponseBody := CreateObject('Chilkat_9_5_0.StringBuilder')
lnSuccess := loRest:FullRequestSb("GET","/api/v3/sdc/commands",loSbRequestBody,loSbResponseBody)
IF lnSuccess <> 1
msgbox( loRest:LastErrorText,'Informacija')
RELEASE loRest
RELEASE loJson
RELEASE loSbResponseBody
Return nil
ENDIF
nstatus = loRest:ResponseStatusCode //--status
cResponse:=loSbResponseBody:GetAsString() //--response
If nStatus=200
*--OK
Else
*--Error
Endif

This works fine on both windows OS but always returns a 401 status (not autorized)
This is because i don't know how to insert authorization: oHC:SetCertificate('user\MY\'+alltrim(pcSubjectCN))

The matter is urgent and please if anyone knows the answer.

Re: HttpClient() Chilkat replacement code

Posted: Mon Sep 02, 2024 2:36 am
by reganc

Re: HttpClient() Chilkat replacement code

Posted: Sun Sep 22, 2024 9:13 am
by rdonnay
I have problems with setCertificate() too with a server I wrote for a customer.

For now, we have to access the endpoint methods via a VPN and Http://. No Https://

What did you find out?

Re: HttpClient() Chilkat replacement code

Posted: Mon Sep 23, 2024 11:01 am
by jezda
Roger,
I found a Chilkat solution for this and it works very well...

LOCAL pcSubjectCN:='ABCD Megasoft'
LOCAL cUr:l='https://api.sandbox.suf.purs.gov.rs/api/v3/sdc/commands'

loHttp := CreateObject('Chilkat_9_5_0.Http')

*--certificate from card (working....)
*loCert := CreateObject('Chilkat_9_5_0.Cert') //--v1
*lnSuccess := loCert:LoadFromSmartcard("") //--v1
*lnSuccess := loHttp:SetSslClientCert(loCert) //--v1

*--certificate from windows certificate store (also working...)
loCert := CreateObject('Chilkat_9_5_0.Cert') //--v2
lnSuccess := loCert:LoadByCommonName(alltrim(pcSubjectCN)) //--v2
lnSuccess := loHttp:SetSslClientCert(loCert) //--v2

*--
loHttp:SetRequestHeader("accept","application/json")
loHttp:SetRequestHeader("Content-Type","application/json")
loHttp:SetRequestHeader("TaxCoreAuthenticationToken",pcToken)
*--
loSbResponseBody := CreateObject('Chilkat_9_5_0.StringBuilder')
lnSuccess := loHttp:QuickGetSb(cUrl,loSbResponseBody)
IF (lnSuccess = 0)
RELEASE loHttp
RELEASE loSbResponseBody
msgbox( loHttp:LastErrorText )
Return nil
ENDIF
*--
nStatus:=loHttp:LastStatus
cResponse:=loSbResponseBody:GetAsString()
If nStatus=200 //--Status OK
JsonFile:=DC_Path(AppName(.t.))+'data\'answerOK.json'
strfile(cResponse,JsonFile,space(0),.f.)
else
JsonFile:=DC_Path(AppName(.t.))+'data\'answerError.json'
strfile(cResponse,JsonFile,space(0),.f.)
Endif
*--
RELEASE loHttp
RELEASE loCert
RELEASE loSbResponseBody