Sample code for 30+ languages & platforms
Unicode C

palena.sii.cl getToken SOAP Request

See more SII Chile Examples

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

Chilkat Unicode C Downloads

Unicode C
#include <C_CkXmlW.h>
#include <C_CkXmlDSigGenW.h>
#include <C_CkCertW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkXmlW xmlToSign;
    HCkXmlDSigGenW gen;
    HCkCertW cert;
    HCkStringBuilderW sbXml;
    HCkHttpW http;
    int responseStatusCode;
    const wchar_t *endPoint;
    HCkXmlW xml;
    HCkStringBuilderW sbSoapXml;
    int numReplaced;
    const wchar_t *xmlStr;
    HCkHttpResponseW resp;
    HCkXmlW xmlResp;

    success = FALSE;

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

    // Create the XML to be signed...
    xmlToSign = CkXmlW_Create();
    CkXmlW_putTag(xmlToSign,L"getToken");
    // This is the seed obtained from palena.sii.cl getSeed
    CkXmlW_UpdateChildContent(xmlToSign,L"item|Semilla",L"033878794660");

    gen = CkXmlDSigGenW_Create();

    CkXmlDSigGenW_putSigLocation(gen,L"getToken");
    CkXmlDSigGenW_putSigLocationMod(gen,0);
    CkXmlDSigGenW_putSigNamespacePrefix(gen,L"");
    CkXmlDSigGenW_putSigNamespaceUri(gen,L"http://www.w3.org/2000/09/xmldsig#");
    CkXmlDSigGenW_putSignedInfoCanonAlg(gen,L"EXCL_C14N");
    CkXmlDSigGenW_putSignedInfoDigestMethod(gen,L"sha1");

    CkXmlDSigGenW_AddSameDocRef(gen,L"",L"sha1",L"",L"",L"");

    // Provide a certificate + private key. (PFX password is test123)
    cert = CkCertW_Create();
    success = CkCertW_LoadPfxFile(cert,L"qa_data/pfx/cert_test123.pfx",L"test123");
    if (success == FALSE) {
        wprintf(L"%s\n",CkCertW_lastErrorText(cert));
        CkXmlW_Dispose(xmlToSign);
        CkXmlDSigGenW_Dispose(gen);
        CkCertW_Dispose(cert);
        return;
    }

    CkXmlDSigGenW_SetX509Cert(gen,cert,TRUE);

    CkXmlDSigGenW_putKeyInfoType(gen,L"X509Data");
    CkXmlDSigGenW_putX509Type(gen,L"Certificate");

    // Load XML to be signed...
    sbXml = CkStringBuilderW_Create();
    CkXmlW_putEmitXmlDecl(xmlToSign,FALSE);
    CkXmlW_GetXmlSb(xmlToSign,sbXml);

    CkXmlDSigGenW_putBehaviors(gen,L"IndentedSignature");

    // Sign the XML...
    success = CkXmlDSigGenW_CreateXmlDSigSb(gen,sbXml);
    if (success == FALSE) {
        wprintf(L"%s\n",CkXmlDSigGenW_lastErrorText(gen));
        CkXmlW_Dispose(xmlToSign);
        CkXmlDSigGenW_Dispose(gen);
        CkCertW_Dispose(cert);
        CkStringBuilderW_Dispose(sbXml);
        return;
    }

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

    http = CkHttpW_Create();

    CkHttpW_putUncommonOptions(http,L"AllowEmptyHeaders");
    CkHttpW_SetRequestHeader(http,L"SOAPAction",L"");

    // The endpoint for this soap service is:
    endPoint = L"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>

    xml = CkXmlW_Create();
    CkXmlW_putTag(xml,L"soapenv:Envelope");
    CkXmlW_AddAttribute(xml,L"xmlns:xsi",L"http://www.w3.org/2001/XMLSchema-instance");
    CkXmlW_AddAttribute(xml,L"xmlns:xsd",L"http://www.w3.org/2001/XMLSchema");
    CkXmlW_AddAttribute(xml,L"xmlns:soapenv",L"http://schemas.xmlsoap.org/soap/envelope/");
    CkXmlW_AddAttribute(xml,L"xmlns:def",L"http://DefaultNamespace");
    CkXmlW_UpdateChildContent(xml,L"soapenv:Header",L"");
    CkXmlW_UpdateAttrAt(xml,L"soapenv:Body|def:getToken",TRUE,L"soapenv:encodingStyle",L"http://schemas.xmlsoap.org/soap/encoding/");
    CkXmlW_UpdateChildContent(xml,L"soapenv:Body|def:getToken|pszXml",L"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.
    sbSoapXml = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbSoapXml,CkXmlW_getXml(xml));
    numReplaced = CkStringBuilderW_Replace(sbXml,L"&",L"&amp;");
    numReplaced = CkStringBuilderW_Replace(sbXml,L">",L"&gt;");
    numReplaced = CkStringBuilderW_Replace(sbXml,L"<",L"&lt;");
    numReplaced = CkStringBuilderW_Replace(sbXml,L"\"",L"&quot;");
    numReplaced = CkStringBuilderW_Replace(sbSoapXml,L"SIGNED_XML_GOES_HERE",CkStringBuilderW_getAsString(sbXml));

    xmlStr = CkStringBuilderW_getAsString(sbSoapXml);
    resp = CkHttpResponseW_Create();
    success = CkHttpW_HttpStr(http,L"POST",endPoint,xmlStr,L"utf-8",L"text/xml",resp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        CkXmlW_Dispose(xmlToSign);
        CkXmlDSigGenW_Dispose(gen);
        CkCertW_Dispose(cert);
        CkStringBuilderW_Dispose(sbXml);
        CkHttpW_Dispose(http);
        CkXmlW_Dispose(xml);
        CkStringBuilderW_Dispose(sbSoapXml);
        CkHttpResponseW_Dispose(resp);
        return;
    }

    responseStatusCode = CkHttpResponseW_getStatusCode(resp);

    wprintf(L"Response Status Code: %d\n",responseStatusCode);

    // You may examine the exact HTTP header sent with the POST like this:
    wprintf(L"LastHeader:\n");
    wprintf(L"%s\n",CkHttpW_lastHeader(http));

    // Examine the XML returned by the web service:
    wprintf(L"XML Response:\n");
    xmlResp = CkXmlW_Create();
    CkXmlW_LoadXml(xmlResp,CkHttpResponseW_bodyStr(resp));
    wprintf(L"%s\n",CkXmlW_getXml(xmlResp));

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


    CkXmlW_Dispose(xmlToSign);
    CkXmlDSigGenW_Dispose(gen);
    CkCertW_Dispose(cert);
    CkStringBuilderW_Dispose(sbXml);
    CkHttpW_Dispose(http);
    CkXmlW_Dispose(xml);
    CkStringBuilderW_Dispose(sbSoapXml);
    CkHttpResponseW_Dispose(resp);
    CkXmlW_Dispose(xmlResp);

    }