Sample code for 30+ languages & platforms
C

palena.sii.cl getToken SOAP Request

See more SII Chile Examples

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

Chilkat C Downloads

C
#include <C_CkXml.h>
#include <C_CkXmlDSigGen.h>
#include <C_CkCert.h>
#include <C_CkStringBuilder.h>
#include <C_CkHttp.h>
#include <C_CkHttpResponse.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkXml xmlToSign;
    HCkXmlDSigGen gen;
    HCkCert cert;
    HCkStringBuilder sbXml;
    HCkHttp http;
    int responseStatusCode;
    const char *endPoint;
    HCkXml xml;
    HCkStringBuilder sbSoapXml;
    int numReplaced;
    const char *xmlStr;
    HCkHttpResponse resp;
    HCkXml 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 = CkXml_Create();
    CkXml_putTag(xmlToSign,"getToken");
    // This is the seed obtained from palena.sii.cl getSeed
    CkXml_UpdateChildContent(xmlToSign,"item|Semilla","033878794660");

    gen = CkXmlDSigGen_Create();

    CkXmlDSigGen_putSigLocation(gen,"getToken");
    CkXmlDSigGen_putSigLocationMod(gen,0);
    CkXmlDSigGen_putSigNamespacePrefix(gen,"");
    CkXmlDSigGen_putSigNamespaceUri(gen,"http://www.w3.org/2000/09/xmldsig#");
    CkXmlDSigGen_putSignedInfoCanonAlg(gen,"EXCL_C14N");
    CkXmlDSigGen_putSignedInfoDigestMethod(gen,"sha1");

    CkXmlDSigGen_AddSameDocRef(gen,"","sha1","","","");

    // Provide a certificate + private key. (PFX password is test123)
    cert = CkCert_Create();
    success = CkCert_LoadPfxFile(cert,"qa_data/pfx/cert_test123.pfx","test123");
    if (success == FALSE) {
        printf("%s\n",CkCert_lastErrorText(cert));
        CkXml_Dispose(xmlToSign);
        CkXmlDSigGen_Dispose(gen);
        CkCert_Dispose(cert);
        return;
    }

    CkXmlDSigGen_SetX509Cert(gen,cert,TRUE);

    CkXmlDSigGen_putKeyInfoType(gen,"X509Data");
    CkXmlDSigGen_putX509Type(gen,"Certificate");

    // Load XML to be signed...
    sbXml = CkStringBuilder_Create();
    CkXml_putEmitXmlDecl(xmlToSign,FALSE);
    CkXml_GetXmlSb(xmlToSign,sbXml);

    CkXmlDSigGen_putBehaviors(gen,"IndentedSignature");

    // Sign the XML...
    success = CkXmlDSigGen_CreateXmlDSigSb(gen,sbXml);
    if (success == FALSE) {
        printf("%s\n",CkXmlDSigGen_lastErrorText(gen));
        CkXml_Dispose(xmlToSign);
        CkXmlDSigGen_Dispose(gen);
        CkCert_Dispose(cert);
        CkStringBuilder_Dispose(sbXml);
        return;
    }

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

    http = CkHttp_Create();

    CkHttp_putUncommonOptions(http,"AllowEmptyHeaders");
    CkHttp_SetRequestHeader(http,"SOAPAction","");

    // The endpoint for this soap service is:
    endPoint = "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 = CkXml_Create();
    CkXml_putTag(xml,"soapenv:Envelope");
    CkXml_AddAttribute(xml,"xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
    CkXml_AddAttribute(xml,"xmlns:xsd","http://www.w3.org/2001/XMLSchema");
    CkXml_AddAttribute(xml,"xmlns:soapenv","http://schemas.xmlsoap.org/soap/envelope/");
    CkXml_AddAttribute(xml,"xmlns:def","http://DefaultNamespace");
    CkXml_UpdateChildContent(xml,"soapenv:Header","");
    CkXml_UpdateAttrAt(xml,"soapenv:Body|def:getToken",TRUE,"soapenv:encodingStyle","http://schemas.xmlsoap.org/soap/encoding/");
    CkXml_UpdateChildContent(xml,"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.
    sbSoapXml = CkStringBuilder_Create();
    CkStringBuilder_Append(sbSoapXml,CkXml_getXml(xml));
    numReplaced = CkStringBuilder_Replace(sbXml,"&","&amp;");
    numReplaced = CkStringBuilder_Replace(sbXml,">","&gt;");
    numReplaced = CkStringBuilder_Replace(sbXml,"<","&lt;");
    numReplaced = CkStringBuilder_Replace(sbXml,"\"","&quot;");
    numReplaced = CkStringBuilder_Replace(sbSoapXml,"SIGNED_XML_GOES_HERE",CkStringBuilder_getAsString(sbXml));

    xmlStr = CkStringBuilder_getAsString(sbSoapXml);
    resp = CkHttpResponse_Create();
    success = CkHttp_HttpStr(http,"POST",endPoint,xmlStr,"utf-8","text/xml",resp);
    if (success == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkXml_Dispose(xmlToSign);
        CkXmlDSigGen_Dispose(gen);
        CkCert_Dispose(cert);
        CkStringBuilder_Dispose(sbXml);
        CkHttp_Dispose(http);
        CkXml_Dispose(xml);
        CkStringBuilder_Dispose(sbSoapXml);
        CkHttpResponse_Dispose(resp);
        return;
    }

    responseStatusCode = CkHttpResponse_getStatusCode(resp);

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

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

    // Examine the XML returned by the web service:
    printf("XML Response:\n");
    xmlResp = CkXml_Create();
    CkXml_LoadXml(xmlResp,CkHttpResponse_bodyStr(resp));
    printf("%s\n",CkXml_getXml(xmlResp));

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


    CkXml_Dispose(xmlToSign);
    CkXmlDSigGen_Dispose(gen);
    CkCert_Dispose(cert);
    CkStringBuilder_Dispose(sbXml);
    CkHttp_Dispose(http);
    CkXml_Dispose(xml);
    CkStringBuilder_Dispose(sbSoapXml);
    CkHttpResponse_Dispose(resp);
    CkXml_Dispose(xmlResp);

    }