Sample code for 30+ languages & platforms
C

HTTP request for a SOAP web service using WS-Security 1.0 with a digital certificate for authentication

See more HTTP Examples

Demonstrates how to build and send an HTTP request for a SOAP web service using WS-Security 1.0 with a digital certificate for authentication.

Chilkat C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkCert cert;
    HCkXml xml;
    HCkXmlDSigGen gen;
    HCkXml xmlCustomKeyInfo;
    HCkStringBuilder sbXml;
    HCkHttp http;
    HCkHttpResponse resp;

    success = FALSE;

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

    //  See HTTP request for a SOAP web service using WS-Security 1.0 with a digital certificate for authentication

    //  ------------------------------------
    //  Step 1: Load the signing certificate
    //  ------------------------------------
    cert = CkCert_Create();
    success = CkCert_LoadFromSmartcard(cert,"");
    if (success == FALSE) {
        printf("%s\n",CkCert_lastErrorText(cert));
        CkCert_Dispose(cert);
        return;
    }

    //  ---------------------------------------
    //  Step 2: Build the SOAP XML to be signed
    //  ---------------------------------------

    xml = CkXml_Create();
    CkXml_putTag(xml,"SOAP-ENV:Envelope");
    CkXml_AddAttribute(xml,"xmlns:SOAP-ENV","http://schemas.xmlsoap.org/soap/envelope/");
    CkXml_AddAttribute(xml,"xmlns:web","http://www.example.com/webservice/");
    CkXml_UpdateAttrAt(xml,"SOAP-ENV:Header|wsse:Security",TRUE,"xmlns:ds","http://www.w3.org/2000/09/xmldsig#");
    CkXml_UpdateAttrAt(xml,"SOAP-ENV:Header|wsse:Security",TRUE,"xmlns:wsse","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
    CkXml_UpdateAttrAt(xml,"SOAP-ENV:Header|wsse:Security",TRUE,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
    CkXml_UpdateAttrAt(xml,"SOAP-ENV:Header|wsse:Security",TRUE,"xmlns:xenc","http://www.w3.org/2001/04/xmlenc#");
    CkXml_UpdateAttrAt(xml,"SOAP-ENV:Header|wsse:Security",TRUE,"SOAP-ENV:mustUnderstand","1");
    CkXml_UpdateAttrAt(xml,"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,"A1","");
    CkXml_UpdateAttrAt(xml,"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,"EncodingType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
    CkXml_UpdateAttrAt(xml,"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,"ValueType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509");
    CkXml_UpdateAttrAt(xml,"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,"wsu:Id","x509cert00");
    CkXml_UpdateChildContent(xml,"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",CkCert_getEncoded(cert));
    CkXml_UpdateAttrAt(xml,"SOAP-ENV:Body",TRUE,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
    CkXml_UpdateAttrAt(xml,"SOAP-ENV:Body",TRUE,"wsu:Id","TheBody");
    CkXml_UpdateChildContent(xml,"SOAP-ENV:Body|web:MyRequest|web:Parameter","MyValue");

    //  ---------------------------------------
    //  Step 3: Sign the XML
    //  ---------------------------------------

    gen = CkXmlDSigGen_Create();

    CkXmlDSigGen_putSigLocation(gen,"SOAP-ENV:Envelope|SOAP-ENV:Header|wsse:Security");
    CkXmlDSigGen_putSigLocationMod(gen,0);
    CkXmlDSigGen_putSigNamespacePrefix(gen,"ds");
    CkXmlDSigGen_putSigNamespaceUri(gen,"http://www.w3.org/2000/09/xmldsig#");
    CkXmlDSigGen_putSignedInfoPrefixList(gen,"ds wsu xenc SOAP-ENV ");
    CkXmlDSigGen_putIncNamespacePrefix(gen,"c14n");
    CkXmlDSigGen_putIncNamespaceUri(gen,"http://www.w3.org/2001/10/xml-exc-c14n#");
    CkXmlDSigGen_putSignedInfoCanonAlg(gen,"EXCL_C14N");
    CkXmlDSigGen_putSignedInfoDigestMethod(gen,"sha1");

    //  -------- Reference 1 --------
    CkXmlDSigGen_AddSameDocRef(gen,"TheBody","sha1","EXCL_C14N","wsu SOAP-ENV","");

    CkXmlDSigGen_SetX509Cert(gen,cert,TRUE);

    CkXmlDSigGen_putKeyInfoType(gen,"Custom");

    //  Create the custom KeyInfo XML..
    xmlCustomKeyInfo = CkXml_Create();
    CkXml_putTag(xmlCustomKeyInfo,"wsse:SecurityTokenReference");
    CkXml_putContent(xmlCustomKeyInfo,"5");
    CkXml_UpdateAttrAt(xmlCustomKeyInfo,"wsse:Reference",TRUE,"URI","#x509cert00");
    CkXml_UpdateAttrAt(xmlCustomKeyInfo,"wsse:Reference",TRUE,"ValueType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509");

    CkXml_putEmitXmlDecl(xmlCustomKeyInfo,FALSE);
    CkXmlDSigGen_putCustomKeyInfoXml(gen,CkXml_getXml(xmlCustomKeyInfo));

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

    CkXmlDSigGen_putBehaviors(gen,"IndentedSignature");

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

    //  ---------------------------------------------------------
    //  Step 4: Send the HTTP POST containing the Signed SOAP XML
    //  ---------------------------------------------------------

    http = CkHttp_Create();

    CkHttp_SetRequestHeader(http,"SOAPAction","\"http://www.example.com/MyWebService\"");
    CkHttp_SetRequestHeader(http,"Host","www.example.com");
    CkHttp_SetRequestHeader(http,"Content-Type","text/xml; charset=utf-8");

    resp = CkHttpResponse_Create();
    success = CkHttp_HttpSb(http,"POST","https://example.com/MyWebService",sbXml,"utf-8","text/xml; charset=utf-8",resp);
    if (success == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkCert_Dispose(cert);
        CkXml_Dispose(xml);
        CkXmlDSigGen_Dispose(gen);
        CkXml_Dispose(xmlCustomKeyInfo);
        CkStringBuilder_Dispose(sbXml);
        CkHttp_Dispose(http);
        CkHttpResponse_Dispose(resp);
        return;
    }

    printf("%d\n",CkHttpResponse_getStatusCode(resp));
    printf("%s\n",CkHttpResponse_bodyStr(resp));

    printf("Finished.\n");


    CkCert_Dispose(cert);
    CkXml_Dispose(xml);
    CkXmlDSigGen_Dispose(gen);
    CkXml_Dispose(xmlCustomKeyInfo);
    CkStringBuilder_Dispose(sbXml);
    CkHttp_Dispose(http);
    CkHttpResponse_Dispose(resp);

    }