Sample code for 30+ languages & platforms
Visual FoxPro

palena.sii.cl getToken SOAP Request

See more SII Chile Examples

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

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loXmlToSign
LOCAL loGen
LOCAL loCert
LOCAL loSbXml
LOCAL loHttp
LOCAL lnResponseStatusCode
LOCAL lcEndPoint
LOCAL loXml
LOCAL loSbSoapXml
LOCAL lnNumReplaced
LOCAL lcXmlStr
LOCAL loResp
LOCAL loXmlResp

lnSuccess = 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...
loXmlToSign = CreateObject('Chilkat.Xml')
loXmlToSign.Tag = "getToken"
* This is the seed obtained from palena.sii.cl getSeed
loXmlToSign.UpdateChildContent("item|Semilla","033878794660")

loGen = CreateObject('Chilkat.XmlDSigGen')

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

loGen.AddSameDocRef("","sha1","","","")

* Provide a certificate + private key. (PFX password is test123)
loCert = CreateObject('Chilkat.Cert')
lnSuccess = loCert.LoadPfxFile("qa_data/pfx/cert_test123.pfx","test123")
IF (lnSuccess = 0) THEN
    ? loCert.LastErrorText
    RELEASE loXmlToSign
    RELEASE loGen
    RELEASE loCert
    CANCEL
ENDIF

loGen.SetX509Cert(loCert,1)

loGen.KeyInfoType = "X509Data"
loGen.X509Type = "Certificate"

* Load XML to be signed...
loSbXml = CreateObject('Chilkat.StringBuilder')
loXmlToSign.EmitXmlDecl = 0
loXmlToSign.GetXmlSb(loSbXml)

loGen.Behaviors = "IndentedSignature"

* Sign the XML...
lnSuccess = loGen.CreateXmlDSigSb(loSbXml)
IF (lnSuccess = 0) THEN
    ? loGen.LastErrorText
    RELEASE loXmlToSign
    RELEASE loGen
    RELEASE loCert
    RELEASE loSbXml
    CANCEL
ENDIF

* -----------------------------------------------

loHttp = CreateObject('Chilkat.Http')

loHttp.UncommonOptions = "AllowEmptyHeaders"
loHttp.SetRequestHeader("SOAPAction","")

* The endpoint for this soap service is:
lcEndPoint = "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>

loXml = CreateObject('Chilkat.Xml')
loXml.Tag = "soapenv:Envelope"
loXml.AddAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance")
loXml.AddAttribute("xmlns:xsd","http://www.w3.org/2001/XMLSchema")
loXml.AddAttribute("xmlns:soapenv","http://schemas.xmlsoap.org/soap/envelope/")
loXml.AddAttribute("xmlns:def","http://DefaultNamespace")
loXml.UpdateChildContent("soapenv:Header","")
loXml.UpdateAttrAt("soapenv:Body|def:getToken",1,"soapenv:encodingStyle","http://schemas.xmlsoap.org/soap/encoding/")
loXml.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.
loSbSoapXml = CreateObject('Chilkat.StringBuilder')
loSbSoapXml.Append(loXml.GetXml())
lnNumReplaced = loSbXml.Replace("&","&amp;")
lnNumReplaced = loSbXml.Replace(">","&gt;")
lnNumReplaced = loSbXml.Replace("<","&lt;")
lnNumReplaced = loSbXml.Replace('"',"&quot;")
lnNumReplaced = loSbSoapXml.Replace("SIGNED_XML_GOES_HERE",loSbXml.GetAsString())

lcXmlStr = loSbSoapXml.GetAsString()
loResp = CreateObject('Chilkat.HttpResponse')
lnSuccess = loHttp.HttpStr("POST",lcEndPoint,lcXmlStr,"utf-8","text/xml",loResp)
IF (lnSuccess = 0) THEN
    ? loHttp.LastErrorText
    RELEASE loXmlToSign
    RELEASE loGen
    RELEASE loCert
    RELEASE loSbXml
    RELEASE loHttp
    RELEASE loXml
    RELEASE loSbSoapXml
    RELEASE loResp
    CANCEL
ENDIF

lnResponseStatusCode = loResp.StatusCode

? "Response Status Code: " + STR(lnResponseStatusCode)

* You may examine the exact HTTP header sent with the POST like this:
? "LastHeader:"
? loHttp.LastHeader

* Examine the XML returned by the web service:
? "XML Response:"
loXmlResp = CreateObject('Chilkat.Xml')
loXmlResp.LoadXml(loResp.BodyStr)
? loXmlResp.GetXml()

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

RELEASE loXmlToSign
RELEASE loGen
RELEASE loCert
RELEASE loSbXml
RELEASE loHttp
RELEASE loXml
RELEASE loSbSoapXml
RELEASE loResp
RELEASE loXmlResp