Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

Unicode C++ Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text

HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(Unicode C++) Create Signed SOAP XML for Albanian Fiscalization Service

Important: This example requires Chilkat v9.5.0.80 or greater.

This example is created for a customer that needs to send signed billing/invoicing SOAP XML requests to http://efiskalizimi-test.tatime.gov.al:80/FiscalizationService

The example demonstrates how to create and signed the SOAP XML message that is to be sent.

Chilkat C/C++ Library Downloads

MS Visual C/C++

Linux/CentOS C/C++

Alpine Linux C/C++

MAC OS X C/C++

armhf/aarch64 C/C++

C++ Builder

iOS C/C++

Android C/C++

Win Mobile 5.0/Pocket PC 2003

Solaris C/C++

FreeBSD C/C++

OpenBSD C/C++

MinGW C/C++

#include <CkXmlW.h>
#include <CkXmlDSigGenW.h>
#include <CkCertW.h>
#include <CkStringBuilderW.h>
#include <CkHttpW.h>
#include <CkHttpResponseW.h>

void ChilkatSample(void)
    {
    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // We want to build and send a signed SOAP XML that looks like the following:

    // 
    // <?xml version="1.0" encoding="utf-8"?>
    // <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    //     <SOAP-ENV:Header/>
    //     <SOAP-ENV:Body>
    //         <RegisterWTNRequest xmlns="https://eFiskalizimi.tatime.gov.al/FiscalizationService/schema" xmlns:ns2="http://www.w3.org/2000/09/xmldsig#" Id="Request">
    //             <Header SendDateTime="2019-09-03T14:19:01+02:00" UUID="42db3af5-0d9f-4dea-95b4-4b947ab8d8e7"/>
    //             <WTN BusinUnit="bb123bb123" DateTimeCreated="2019-09-03T14:19:01+02:00" DestinAddr="Destination address" 
    // 			DestinCity="Destination city" IsAfterDel="false" OperatorCode="oo123oo123" 
    // 			SoftNum="ss123ss123" StartAddr="Start address" StartCity="Start city" TransDate="2019-09-03T14:19:01+02:00" 
    // 			VehPlates="AA0000AA" WTNIC="5C5E58580D0A24E1F7A5E5E011929511" WTNICSignature="82D69C38206D ... F5FCA48" WTNNum="12345678901">
    //                 <Issuer NUIS="I12345678I" Name="Issuer name"/>
    //                 <Items>
    //                     <I C="501234567890" N="Item name" Q="1.0" U="piece"/>
    //                 </Items>
    //             </WTN>
    //             <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
    //                 <SignedInfo>
    //                     <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
    //                     <SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
    //                     <Reference URI="#Request">
    //                         <Transforms>
    //                             <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
    //                             <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
    //                         </Transforms>
    //                         <DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
    //                         <DigestValue>aNa0r4RW04BlsAciTUrWHGW9PADskl2op315BAQ0hTg=</DigestValue>
    //                     </Reference>
    //                 </SignedInfo>
    //                 <SignatureValue>4/j/d4j/5xCJ2YUP+XTC6li0B94...........................KESd38NT5+puArBcNgLYIjLx/dh6Q==</SignatureValue>
    //                 <KeyInfo>
    //                     <X509Data>
    //                         <X509Certificate>MIIE6TCCAtGgAwIBAgICEA8wDQYJKoZIhvcNA......................uoqWsSuLmA==</X509Certificate>
    //                     </X509Data>
    //                 </KeyInfo>
    //             </Signature>
    //         </RegisterWTNRequest>
    //     </SOAP-ENV:Body>
    // </SOAP-ENV:Envelope>

    bool success = true;
    // Create the XML to be signed...
    CkXmlW xmlToSign;
    xmlToSign.put_Tag(L"SOAP-ENV:Envelope");
    xmlToSign.AddAttribute(L"xmlns:SOAP-ENV",L"http://schemas.xmlsoap.org/soap/envelope/");
    xmlToSign.UpdateChildContent(L"SOAP-ENV:Header",L"");
    xmlToSign.UpdateAttrAt(L"SOAP-ENV:Body|RegisterWTNRequest",true,L"xmlns",L"https://eFiskalizimi.tatime.gov.al/FiscalizationService/schema");
    xmlToSign.UpdateAttrAt(L"SOAP-ENV:Body|RegisterWTNRequest",true,L"xmlns:ns2",L"http://www.w3.org/2000/09/xmldsig#");
    xmlToSign.UpdateAttrAt(L"SOAP-ENV:Body|RegisterWTNRequest",true,L"Id",L"Request");
    xmlToSign.UpdateAttrAt(L"SOAP-ENV:Body|RegisterWTNRequest|Header",true,L"SendDateTime",L"2019-09-03T14:19:01+02:00");
    xmlToSign.UpdateAttrAt(L"SOAP-ENV:Body|RegisterWTNRequest|Header",true,L"UUID",L"42db3af5-0d9f-4dea-95b4-4b947ab8d8e7");
    xmlToSign.UpdateAttrAt(L"SOAP-ENV:Body|RegisterWTNRequest|WTN",true,L"BusinUnit",L"bb123bb123");
    xmlToSign.UpdateAttrAt(L"SOAP-ENV:Body|RegisterWTNRequest|WTN",true,L"DateTimeCreated",L"2019-09-03T14:19:01+02:00");
    xmlToSign.UpdateAttrAt(L"SOAP-ENV:Body|RegisterWTNRequest|WTN",true,L"DestinAddr",L"Destination address");
    xmlToSign.UpdateAttrAt(L"SOAP-ENV:Body|RegisterWTNRequest|WTN",true,L"DestinCity",L"Destination city");
    xmlToSign.UpdateAttrAt(L"SOAP-ENV:Body|RegisterWTNRequest|WTN",true,L"IsAfterDel",L"false");
    xmlToSign.UpdateAttrAt(L"SOAP-ENV:Body|RegisterWTNRequest|WTN",true,L"OperatorCode",L"oo123oo123");
    xmlToSign.UpdateAttrAt(L"SOAP-ENV:Body|RegisterWTNRequest|WTN",true,L"SoftNum",L"ss123ss123");
    xmlToSign.UpdateAttrAt(L"SOAP-ENV:Body|RegisterWTNRequest|WTN",true,L"StartAddr",L"Start address");
    xmlToSign.UpdateAttrAt(L"SOAP-ENV:Body|RegisterWTNRequest|WTN",true,L"StartCity",L"Start city");
    xmlToSign.UpdateAttrAt(L"SOAP-ENV:Body|RegisterWTNRequest|WTN",true,L"TransDate",L"2019-09-03T14:19:01+02:00");
    xmlToSign.UpdateAttrAt(L"SOAP-ENV:Body|RegisterWTNRequest|WTN",true,L"VehPlates",L"AA0000AA");
    xmlToSign.UpdateAttrAt(L"SOAP-ENV:Body|RegisterWTNRequest|WTN",true,L"WTNIC",L"5C5E58580D0A24E1F7A5E5E011929511");
    xmlToSign.UpdateAttrAt(L"SOAP-ENV:Body|RegisterWTNRequest|WTN",true,L"WTNICSignature",L"82D69C38206D ... F5FCA48");
    xmlToSign.UpdateAttrAt(L"SOAP-ENV:Body|RegisterWTNRequest|WTN",true,L"WTNNum",L"12345678901");
    xmlToSign.UpdateAttrAt(L"SOAP-ENV:Body|RegisterWTNRequest|WTN|Issuer",true,L"NUIS",L"I12345678I");
    xmlToSign.UpdateAttrAt(L"SOAP-ENV:Body|RegisterWTNRequest|WTN|Issuer",true,L"Name",L"Issuer name");
    xmlToSign.UpdateAttrAt(L"SOAP-ENV:Body|RegisterWTNRequest|WTN|Items|I",true,L"C",L"501234567890");
    xmlToSign.UpdateAttrAt(L"SOAP-ENV:Body|RegisterWTNRequest|WTN|Items|I",true,L"N",L"Item name");
    xmlToSign.UpdateAttrAt(L"SOAP-ENV:Body|RegisterWTNRequest|WTN|Items|I",true,L"Q",L"1.0");
    xmlToSign.UpdateAttrAt(L"SOAP-ENV:Body|RegisterWTNRequest|WTN|Items|I",true,L"U",L"piece");

    CkXmlDSigGenW gen;

    gen.put_SigLocation(L"SOAP-ENV:Envelope|SOAP-ENV:Body|RegisterWTNRequest");
    gen.put_SigLocationMod(0);
    gen.put_SigNamespacePrefix(L"");
    gen.put_SigNamespaceUri(L"http://www.w3.org/2000/09/xmldsig#");
    gen.put_SignedInfoCanonAlg(L"EXCL_C14N");
    gen.put_SignedInfoDigestMethod(L"sha256");

    // -------- Reference 1 --------
    gen.AddSameDocRef(L"Request",L"sha256",L"EXCL_C14N",L"",L"");

    // Provide a certificate + private key. (PFX password is test123)
    CkCertW cert;
    success = cert.LoadPfxFile(L"qa_data/pfx/cert_test123.pfx",L"test123");
    if (success != true) {
        wprintf(L"%s\n",cert.lastErrorText());
        return;
    }

    gen.SetX509Cert(cert,true);

    gen.put_KeyInfoType(L"X509Data");
    gen.put_X509Type(L"Certificate");

    // Load XML to be signed...
    CkStringBuilderW sbXml;
    xmlToSign.GetXmlSb(sbXml);

    gen.put_Behaviors(L"CompactSignedXml,ForceAddEnvelopedSignatureTransform");

    // Sign the XML...
    success = gen.CreateXmlDSigSb(sbXml);
    if (success != true) {
        wprintf(L"%s\n",gen.lastErrorText());
        return;
    }

    // -----------------------------------------------
    // Now to send the SOAP request....

    // Note: Chilkat did not actually test this by sending the request to the server.
    // The example is a best-guess for how to do it...

    const wchar_t *strXml = sbXml.getAsString();

    int responseStatusCode;

    // We'll need to add this in the HTTP header:
    // SOAPAction: "https://eFiskalizimi.tatime.gov.al/FiscalizationService/RegisterWTN"

    // Note: This is for the RegisterWTN request.

    // You may wish to load the WSDL at https://efiskalizimi-test.tatime.gov.al/FiscalizationService-v1/FiscalizationService.wsdl into the SoapUI application to see 
    // the SOAP requests..
    CkHttpW http;
    http.SetRequestHeader(L"SOAPAction",L"https://eFiskalizimi.tatime.gov.al/FiscalizationService/RegisterWTN");

    // Some services expect the content-type in the HTTP header to be "application/xml" while
    // other expect text/xml.  The default sent by Chilkat is "application/xml", but this web service
    // likely expects "text/xml".  Therefore, change the content-type:
    http.SetRequestHeader(L"Content-Type",L"text/xml; charset=utf-8");

    // The testing endpoint for this soap service is likely:
    const wchar_t *endPoint = L"https://efiskalizimi-test.tatime.gov.al/FiscalizationService-v1";

    CkHttpResponseW *resp = http.PostXml(endPoint,strXml,L"utf-8");
    if (http.get_LastMethodSuccess() != true) {
        wprintf(L"%s\n",http.lastErrorText());
        wprintf(L"LastHeader:\n");
        wprintf(L"%s\n",http.lastHeader());
        return;
    }

    responseStatusCode = resp->get_StatusCode();

    // Check that the responseStatusCode equals 200...
    wprintf(L"Response Status Code: %d\n",responseStatusCode);

    // Examine the exact HTTP header sent with the POST like this:
    wprintf(L"LastHeader:\n");
    wprintf(L"%s\n",http.lastHeader());

    // Examine the XML returned by the web service:
    wprintf(L"XML Response:\n");
    CkXmlW xmlResp;
    xmlResp.LoadXml(resp->bodyStr());
    wprintf(L"%s\n",xmlResp.getXml());

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

    delete resp;
    }

 

© 2000-2019 Chilkat Software, Inc. All Rights Reserved.