HttpClient() Chilkat replacement code

This forum is for eXpress++ general support.
Post Reply
Message
Author
jezda
Posts: 17
Joined: Thu Mar 28, 2019 4:41 am

HttpClient() Chilkat replacement code

#1 Post 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.

reganc
Posts: 259
Joined: Thu Jan 28, 2010 3:08 am
Location: Hersham, Surrey, UK
Contact:

Re: HttpClient() Chilkat replacement code

#2 Post by reganc »

Regan Cawkwell
Real Business Applications Ltd
http://www.rbauk.com

User avatar
rdonnay
Site Admin
Posts: 4774
Joined: Wed Jan 27, 2010 6:58 pm
Location: Boise, Idaho USA
Contact:

Re: HttpClient() Chilkat replacement code

#3 Post 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?
The eXpress train is coming - and it has more cars.

jezda
Posts: 17
Joined: Thu Mar 28, 2019 4:41 am

Re: HttpClient() Chilkat replacement code

#4 Post 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

Post Reply