Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Delphi DLL) HTTP request for a SOAP web service using WS-Security 1.0 with a digital certificate for authenticationSee more HTTP ExamplesDemonstrates how to build and send an HTTP request for a SOAP web service using WS-Security 1.0 with a digital certificate for authentication.
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 cert: HCkCert; success: Boolean; xml: HCkXml; gen: HCkXmlDSigGen; xmlCustomKeyInfo: HCkXml; sbXml: HCkStringBuilder; http: HCkHttp; resp: HCkHttpResponse; begin // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // // ------------------------------------ // 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 <> True) 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 := CkHttp_PTextSb(http,'POST','https://example.com/MyWebService',sbXml,'utf-8','text/xml; charset=utf-8',False,False); if (CkHttp_getLastMethodSuccess(http) = False) then begin Memo1.Lines.Add(CkHttp__lastErrorText(http)); Exit; end; Memo1.Lines.Add(IntToStr(CkHttpResponse_getStatusCode(resp))); Memo1.Lines.Add(CkHttpResponse__bodyStr(resp)); CkHttpResponse_Dispose(resp); Memo1.Lines.Add('Finished.'); CkCert_Dispose(cert); CkXml_Dispose(xml); CkXmlDSigGen_Dispose(gen); CkXml_Dispose(xmlCustomKeyInfo); CkStringBuilder_Dispose(sbXml); CkHttp_Dispose(http); end; |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.