Sample code for 30+ languages & platforms
Delphi DLL

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 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;
cert: HCkCert;
xml: HCkXml;
gen: HCkXmlDSigGen;
xmlCustomKeyInfo: HCkXml;
sbXml: HCkStringBuilder;
http: HCkHttp;
resp: HCkHttpResponse;

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

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

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

Memo1.Lines.Add(IntToStr(CkHttpResponse_getStatusCode(resp)));
Memo1.Lines.Add(CkHttpResponse__bodyStr(resp));

Memo1.Lines.Add('Finished.');

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

end;