Unicode C
Unicode 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 Unicode C Downloads
#include <C_CkCertW.h>
#include <C_CkXmlW.h>
#include <C_CkXmlDSigGenW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>
void ChilkatSample(void)
{
BOOL success;
HCkCertW cert;
HCkXmlW xml;
HCkXmlDSigGenW gen;
HCkXmlW xmlCustomKeyInfo;
HCkStringBuilderW sbXml;
HCkHttpW http;
HCkHttpResponseW 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 = CkCertW_Create();
success = CkCertW_LoadFromSmartcard(cert,L"");
if (success == FALSE) {
wprintf(L"%s\n",CkCertW_lastErrorText(cert));
CkCertW_Dispose(cert);
return;
}
// ---------------------------------------
// Step 2: Build the SOAP XML to be signed
// ---------------------------------------
xml = CkXmlW_Create();
CkXmlW_putTag(xml,L"SOAP-ENV:Envelope");
CkXmlW_AddAttribute(xml,L"xmlns:SOAP-ENV",L"http://schemas.xmlsoap.org/soap/envelope/");
CkXmlW_AddAttribute(xml,L"xmlns:web",L"http://www.example.com/webservice/");
CkXmlW_UpdateAttrAt(xml,L"SOAP-ENV:Header|wsse:Security",TRUE,L"xmlns:ds",L"http://www.w3.org/2000/09/xmldsig#");
CkXmlW_UpdateAttrAt(xml,L"SOAP-ENV:Header|wsse:Security",TRUE,L"xmlns:wsse",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
CkXmlW_UpdateAttrAt(xml,L"SOAP-ENV:Header|wsse:Security",TRUE,L"xmlns:wsu",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
CkXmlW_UpdateAttrAt(xml,L"SOAP-ENV:Header|wsse:Security",TRUE,L"xmlns:xenc",L"http://www.w3.org/2001/04/xmlenc#");
CkXmlW_UpdateAttrAt(xml,L"SOAP-ENV:Header|wsse:Security",TRUE,L"SOAP-ENV:mustUnderstand",L"1");
CkXmlW_UpdateAttrAt(xml,L"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,L"A1",L"");
CkXmlW_UpdateAttrAt(xml,L"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,L"EncodingType",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
CkXmlW_UpdateAttrAt(xml,L"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,L"ValueType",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509");
CkXmlW_UpdateAttrAt(xml,L"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",TRUE,L"wsu:Id",L"x509cert00");
CkXmlW_UpdateChildContent(xml,L"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",CkCertW_getEncoded(cert));
CkXmlW_UpdateAttrAt(xml,L"SOAP-ENV:Body",TRUE,L"xmlns:wsu",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
CkXmlW_UpdateAttrAt(xml,L"SOAP-ENV:Body",TRUE,L"wsu:Id",L"TheBody");
CkXmlW_UpdateChildContent(xml,L"SOAP-ENV:Body|web:MyRequest|web:Parameter",L"MyValue");
// ---------------------------------------
// Step 3: Sign the XML
// ---------------------------------------
gen = CkXmlDSigGenW_Create();
CkXmlDSigGenW_putSigLocation(gen,L"SOAP-ENV:Envelope|SOAP-ENV:Header|wsse:Security");
CkXmlDSigGenW_putSigLocationMod(gen,0);
CkXmlDSigGenW_putSigNamespacePrefix(gen,L"ds");
CkXmlDSigGenW_putSigNamespaceUri(gen,L"http://www.w3.org/2000/09/xmldsig#");
CkXmlDSigGenW_putSignedInfoPrefixList(gen,L"ds wsu xenc SOAP-ENV ");
CkXmlDSigGenW_putIncNamespacePrefix(gen,L"c14n");
CkXmlDSigGenW_putIncNamespaceUri(gen,L"http://www.w3.org/2001/10/xml-exc-c14n#");
CkXmlDSigGenW_putSignedInfoCanonAlg(gen,L"EXCL_C14N");
CkXmlDSigGenW_putSignedInfoDigestMethod(gen,L"sha1");
// -------- Reference 1 --------
CkXmlDSigGenW_AddSameDocRef(gen,L"TheBody",L"sha1",L"EXCL_C14N",L"wsu SOAP-ENV",L"");
CkXmlDSigGenW_SetX509Cert(gen,cert,TRUE);
CkXmlDSigGenW_putKeyInfoType(gen,L"Custom");
// Create the custom KeyInfo XML..
xmlCustomKeyInfo = CkXmlW_Create();
CkXmlW_putTag(xmlCustomKeyInfo,L"wsse:SecurityTokenReference");
CkXmlW_putContent(xmlCustomKeyInfo,L"5");
CkXmlW_UpdateAttrAt(xmlCustomKeyInfo,L"wsse:Reference",TRUE,L"URI",L"#x509cert00");
CkXmlW_UpdateAttrAt(xmlCustomKeyInfo,L"wsse:Reference",TRUE,L"ValueType",L"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509");
CkXmlW_putEmitXmlDecl(xmlCustomKeyInfo,FALSE);
CkXmlDSigGenW_putCustomKeyInfoXml(gen,CkXmlW_getXml(xmlCustomKeyInfo));
// Load XML to be signed...
sbXml = CkStringBuilderW_Create();
CkXmlW_putEmitXmlDecl(xml,FALSE);
CkXmlW_GetXmlSb(xml,sbXml);
CkXmlDSigGenW_putBehaviors(gen,L"IndentedSignature");
// Sign the XML...
success = CkXmlDSigGenW_CreateXmlDSigSb(gen,sbXml);
if (success == FALSE) {
wprintf(L"%s\n",CkXmlDSigGenW_lastErrorText(gen));
CkCertW_Dispose(cert);
CkXmlW_Dispose(xml);
CkXmlDSigGenW_Dispose(gen);
CkXmlW_Dispose(xmlCustomKeyInfo);
CkStringBuilderW_Dispose(sbXml);
return;
}
// ---------------------------------------------------------
// Step 4: Send the HTTP POST containing the Signed SOAP XML
// ---------------------------------------------------------
http = CkHttpW_Create();
CkHttpW_SetRequestHeader(http,L"SOAPAction",L"\"http://www.example.com/MyWebService\"");
CkHttpW_SetRequestHeader(http,L"Host",L"www.example.com");
CkHttpW_SetRequestHeader(http,L"Content-Type",L"text/xml; charset=utf-8");
resp = CkHttpResponseW_Create();
success = CkHttpW_HttpSb(http,L"POST",L"https://example.com/MyWebService",sbXml,L"utf-8",L"text/xml; charset=utf-8",resp);
if (success == FALSE) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkCertW_Dispose(cert);
CkXmlW_Dispose(xml);
CkXmlDSigGenW_Dispose(gen);
CkXmlW_Dispose(xmlCustomKeyInfo);
CkStringBuilderW_Dispose(sbXml);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
return;
}
wprintf(L"%d\n",CkHttpResponseW_getStatusCode(resp));
wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp));
wprintf(L"Finished.\n");
CkCertW_Dispose(cert);
CkXmlW_Dispose(xml);
CkXmlDSigGenW_Dispose(gen);
CkXmlW_Dispose(xmlCustomKeyInfo);
CkStringBuilderW_Dispose(sbXml);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
}