Sample code for 30+ languages & platforms
Delphi DLL

palena.sii.cl getToken SOAP Request

See more SII Chile Examples

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

Chilkat Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, XmlDSigGen, HttpResponse, StringBuilder, Cert, Http, Xml;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
xmlToSign: HCkXml;
gen: HCkXmlDSigGen;
cert: HCkCert;
sbXml: HCkStringBuilder;
http: HCkHttp;
responseStatusCode: Integer;
endPoint: PWideChar;
xml: HCkXml;
sbSoapXml: HCkStringBuilder;
numReplaced: Integer;
xmlStr: PWideChar;
resp: HCkHttpResponse;
xmlResp: HCkXml;

begin
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) then
  begin
    Memo1.Lines.Add(CkCert__lastErrorText(cert));
    Exit;
  end;

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) then
  begin
    Memo1.Lines.Add(CkXmlDSigGen__lastErrorText(gen));
    Exit;
  end;

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

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) then
  begin
    Memo1.Lines.Add(CkHttp__lastErrorText(http));
    Exit;
  end;

responseStatusCode := CkHttpResponse_getStatusCode(resp);

Memo1.Lines.Add('Response Status Code: ' + IntToStr(responseStatusCode));

// You may examine the exact HTTP header sent with the POST like this:
Memo1.Lines.Add('LastHeader:');
Memo1.Lines.Add(CkHttp__lastHeader(http));

// Examine the XML returned by the web service:
Memo1.Lines.Add('XML Response:');
xmlResp := CkXml_Create();
CkXml_LoadXml(xmlResp,CkHttpResponse__bodyStr(resp));
Memo1.Lines.Add(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);

end;