Sample code for 30+ languages & platforms
Java

palena.sii.cl getToken SOAP Request

See more SII Chile Examples

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

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.StringBuilder;
import com.chilkatsoft.chilkat2.Xml;
import com.chilkatsoft.chilkat2.XmlDSigGen;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        // Create the XML to be signed...
        Xml xmlToSign = new Xml();
        xmlToSign.setTag("getToken");
        // This is the seed obtained from palena.sii.cl getSeed
        xmlToSign.updateChildContent("item|Semilla", "033878794660");

        XmlDSigGen gen = new XmlDSigGen();

        gen.setSigLocation("getToken");
        gen.setSigLocationMod(0);
        gen.setSigNamespacePrefix("");
        gen.setSigNamespaceUri("http://www.w3.org/2000/09/xmldsig#");
        gen.setSignedInfoCanonAlg("EXCL_C14N");
        gen.setSignedInfoDigestMethod("sha1");

        gen.addSameDocRef("", "sha1", "", "", "");

        // Provide a certificate + private key. (PFX password is test123)
        Cert cert = new Cert();
        try {
            cert.loadPfxFile("qa_data/pfx/cert_test123.pfx", "test123");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        gen.setX509Cert(cert, true);

        gen.setKeyInfoType("X509Data");
        gen.setX509Type("Certificate");

        // Load XML to be signed...
        StringBuilder sbXml = new StringBuilder();
        xmlToSign.setEmitXmlDecl(false);
        xmlToSign.getXmlSb(sbXml);

        gen.setBehaviors("IndentedSignature");

        // Sign the XML...
        try {
            gen.createXmlDSigSb(sbXml);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

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

        Http http = new Http();

        http.setUncommonOptions("AllowEmptyHeaders");
        http.setRequestHeader("SOAPAction", "");

        // The endpoint for this soap service is:
        String 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 xml = new Xml();
        xml.setTag("soapenv:Envelope");
        xml.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        xml.addAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
        xml.addAttribute("xmlns:soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
        xml.addAttribute("xmlns:def", "http://DefaultNamespace");
        xml.updateChildContent("soapenv:Header", "");
        xml.updateAttrAt("soapenv:Body|def:getToken", true, "soapenv:encodingStyle", "http://schemas.xmlsoap.org/soap/encoding/");
        xml.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.
        StringBuilder sbSoapXml = new StringBuilder();
        sbSoapXml.append(xml.getXml());
        int numReplaced = sbXml.replace("&", "&amp;");
        numReplaced = sbXml.replace(">", "&gt;");
        numReplaced = sbXml.replace("<", "&lt;");
        numReplaced = sbXml.replace("\"", "&quot;");
        numReplaced = sbSoapXml.replace("SIGNED_XML_GOES_HERE", sbXml.getAsString());

        String xmlStr = sbSoapXml.getAsString();
        HttpResponse resp = new HttpResponse();
        try {
            http.httpStr("POST", endPoint, xmlStr, "utf-8", "text/xml", resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        int responseStatusCode = resp.getStatusCode();

        System.out.println("Response Status Code: " + responseStatusCode);

        // You may examine the exact HTTP header sent with the POST like this:
        System.out.println("LastHeader:");
        System.out.println(http.getLastHeader());

        // Examine the XML returned by the web service:
        System.out.println("XML Response:");
        Xml xmlResp = new Xml();
        xmlResp.loadXml(resp.getBodyStr());
        System.out.println(xmlResp.getXml());

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