Sample code for 30+ languages & platforms
Xbase++

palena.sii.cl getToken SOAP Request

See more SII Chile Examples

Demonstrates how to call getToken SOAP request at palena.sii.cl

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oXmlToSign
LOCAL oGen
LOCAL oCert
LOCAL oSbXml
LOCAL oHttp
LOCAL nResponseStatusCode
LOCAL cEndPoint
LOCAL oXml
LOCAL oSbSoapXml
LOCAL nNumReplaced
LOCAL cXmlStr
LOCAL oResp
LOCAL oXmlResp

nSuccess := 0

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

//  Create the XML to be signed...
oXmlToSign := CreateObject("Chilkat.Xml")
oXmlToSign:Tag := "getToken"
//  This is the seed obtained from palena.sii.cl getSeed
oXmlToSign:UpdateChildContent("item|Semilla", "033878794660")

oGen := CreateObject("Chilkat.XmlDSigGen")

oGen:SigLocation := "getToken"
oGen:SigLocationMod := 0
oGen:SigNamespacePrefix := ""
oGen:SigNamespaceUri := "http://www.w3.org/2000/09/xmldsig#"
oGen:SignedInfoCanonAlg := "EXCL_C14N"
oGen:SignedInfoDigestMethod := "sha1"

oGen:AddSameDocRef("", "sha1", "", "", "")

//  Provide a certificate + private key. (PFX password is test123)
oCert := CreateObject("Chilkat.Cert")
nSuccess := oCert:LoadPfxFile("qa_data/pfx/cert_test123.pfx", "test123")
IF (nSuccess == 0)
    ? oCert:LastErrorText
    oXmlToSign:destroy()
    oGen:destroy()
    oCert:destroy()
    RETURN
ENDIF

oGen:SetX509Cert(oCert, 1)

oGen:KeyInfoType := "X509Data"
oGen:X509Type := "Certificate"

//  Load XML to be signed...
oSbXml := CreateObject("Chilkat.StringBuilder")
oXmlToSign:EmitXmlDecl := 0
oXmlToSign:GetXmlSb(oSbXml)

oGen:Behaviors := "IndentedSignature"

//  Sign the XML...
nSuccess := oGen:CreateXmlDSigSb(oSbXml)
IF (nSuccess == 0)
    ? oGen:LastErrorText
    oXmlToSign:destroy()
    oGen:destroy()
    oCert:destroy()
    oSbXml:destroy()
    RETURN
ENDIF

//  -----------------------------------------------

oHttp := CreateObject("Chilkat.Http")

oHttp:UncommonOptions := "AllowEmptyHeaders"
oHttp:SetRequestHeader("SOAPAction", "")

//  The endpoint for this soap service is:
cEndPoint := "https://palena.sii.cl/DTEWS/GetTokenFromSeed.jws"

//  Send the following SOAP XML

//  <soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:def="http://DefaultNamespace">
//     <soapenv:Header/>
//     <soapenv:Body>
//        <def:getToken soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
//           <pszXml>SIGNED_XML_GOES_HERE</pszXml>
//        </def:getToken>
//     </soapenv:Body>
//  </soapenv:Envelope>

oXml := CreateObject("Chilkat.Xml")
oXml:Tag := "soapenv:Envelope"
oXml:AddAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
oXml:AddAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema")
oXml:AddAttribute("xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope/")
oXml:AddAttribute("xmlns:def", "http://DefaultNamespace")
oXml:UpdateChildContent("soapenv:Header", "")
oXml:UpdateAttrAt("soapenv:Body|def:getToken", 1, "soapenv:encodingStyle", "http://schemas.xmlsoap.org/soap/encoding/")
oXml:UpdateChildContent("soapenv:Body|def:getToken|pszXml", "SIGNED_XML_GOES_HERE")

//  We must replace the "SIGNED_XML_GOES_HERE" with the exact contents of the signed XML to ensure the signed XML is not modified in any way.
oSbSoapXml := CreateObject("Chilkat.StringBuilder")
oSbSoapXml:Append(oXml:GetXml())
nNumReplaced := oSbXml:Replace("&", "&amp;")
nNumReplaced := oSbXml:Replace(">", "&gt;")
nNumReplaced := oSbXml:Replace("<", "&lt;")
nNumReplaced := oSbXml:Replace('"', "&quot;")
nNumReplaced := oSbSoapXml:Replace("SIGNED_XML_GOES_HERE", oSbXml:GetAsString())

cXmlStr := oSbSoapXml:GetAsString()
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpStr("POST", cEndPoint, cXmlStr, "utf-8", "text/xml", oResp)
IF (nSuccess == 0)
    ? oHttp:LastErrorText
    oXmlToSign:destroy()
    oGen:destroy()
    oCert:destroy()
    oSbXml:destroy()
    oHttp:destroy()
    oXml:destroy()
    oSbSoapXml:destroy()
    oResp:destroy()
    RETURN
ENDIF

nResponseStatusCode := oResp:StatusCode

? "Response Status Code: " + Str(nResponseStatusCode)

//  You may examine the exact HTTP header sent with the POST like this:
? "LastHeader:"
? oHttp:LastHeader

//  Examine the XML returned by the web service:
? "XML Response:"
oXmlResp := CreateObject("Chilkat.Xml")
oXmlResp:LoadXml(oResp:BodyStr)
? oXmlResp:GetXml()

//  Use this online tool to generate parsing code from response XML: 
//  Generate Parsing Code from XML

oXmlToSign:destroy()
oGen:destroy()
oCert:destroy()
oSbXml:destroy()
oHttp:destroy()
oXml:destroy()
oSbSoapXml:destroy()
oResp:destroy()
oXmlResp:destroy()