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
(Swift 3,4,5...) 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.
func chilkatTest() { // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // // ------------------------------------ // Step 1: Load the signing certificate // ------------------------------------ let cert = CkoCert()! var success: Bool = cert.load(fromSmartcard: "") if success == false { print("\(cert.lastErrorText!)") return } // --------------------------------------- // Step 2: Build the SOAP XML to be signed // --------------------------------------- let xml = CkoXml()! xml.tag = "SOAP-ENV:Envelope" xml.addAttribute("xmlns:SOAP-ENV", value: "http://schemas.xmlsoap.org/soap/envelope/") xml.addAttribute("xmlns:web", value: "http://www.example.com/webservice/") xml.updateAttr(at: "SOAP-ENV:Header|wsse:Security", autoCreate: true, attrName: "xmlns:ds", attrValue: "http://www.w3.org/2000/09/xmldsig#") xml.updateAttr(at: "SOAP-ENV:Header|wsse:Security", autoCreate: true, attrName: "xmlns:wsse", attrValue: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd") xml.updateAttr(at: "SOAP-ENV:Header|wsse:Security", autoCreate: true, attrName: "xmlns:wsu", attrValue: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd") xml.updateAttr(at: "SOAP-ENV:Header|wsse:Security", autoCreate: true, attrName: "xmlns:xenc", attrValue: "http://www.w3.org/2001/04/xmlenc#") xml.updateAttr(at: "SOAP-ENV:Header|wsse:Security", autoCreate: true, attrName: "SOAP-ENV:mustUnderstand", attrValue: "1") xml.updateAttr(at: "SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken", autoCreate: true, attrName: "A1", attrValue: "") xml.updateAttr(at: "SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken", autoCreate: true, attrName: "EncodingType", attrValue: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary") xml.updateAttr(at: "SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken", autoCreate: true, attrName: "ValueType", attrValue: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509") xml.updateAttr(at: "SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken", autoCreate: true, attrName: "wsu:Id", attrValue: "x509cert00") xml.updateChildContent("SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken", value: cert.getEncoded()) xml.updateAttr(at: "SOAP-ENV:Body", autoCreate: true, attrName: "xmlns:wsu", attrValue: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd") xml.updateAttr(at: "SOAP-ENV:Body", autoCreate: true, attrName: "wsu:Id", attrValue: "TheBody") xml.updateChildContent("SOAP-ENV:Body|web:MyRequest|web:Parameter", value: "MyValue") // --------------------------------------- // Step 3: Sign the XML // --------------------------------------- let gen = CkoXmlDSigGen()! gen.sigLocation = "SOAP-ENV:Envelope|SOAP-ENV:Header|wsse:Security" gen.sigLocationMod = 0 gen.sigNamespacePrefix = "ds" gen.sigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#" gen.signedInfoPrefixList = "ds wsu xenc SOAP-ENV " gen.incNamespacePrefix = "c14n" gen.incNamespaceUri = "http://www.w3.org/2001/10/xml-exc-c14n#" gen.signedInfoCanonAlg = "EXCL_C14N" gen.signedInfoDigestMethod = "sha1" // -------- Reference 1 -------- gen.addSameDocRef("TheBody", digestMethod: "sha1", canonMethod: "EXCL_C14N", prefixList: "wsu SOAP-ENV", refType: "") gen.setX509Cert(cert, usePrivateKey: true) gen.keyInfoType = "Custom" // Create the custom KeyInfo XML.. let xmlCustomKeyInfo = CkoXml()! xmlCustomKeyInfo.tag = "wsse:SecurityTokenReference" xmlCustomKeyInfo.content = "5" xmlCustomKeyInfo.updateAttr(at: "wsse:Reference", autoCreate: true, attrName: "URI", attrValue: "#x509cert00") xmlCustomKeyInfo.updateAttr(at: "wsse:Reference", autoCreate: true, attrName: "ValueType", attrValue: "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509") xmlCustomKeyInfo.emitXmlDecl = false gen.customKeyInfoXml = xmlCustomKeyInfo.getXml() // Load XML to be signed... let sbXml = CkoStringBuilder()! xml.emitXmlDecl = false xml.getSb(sbXml) gen.behaviors = "IndentedSignature" // Sign the XML... success = gen.createXmlDSigSb(sbXml) if success != true { print("\(gen.lastErrorText!)") return } // --------------------------------------------------------- // Step 4: Send the HTTP POST containing the Signed SOAP XML // --------------------------------------------------------- let http = CkoHttp()! http.setRequestHeader("SOAPAction", value: "\"http://www.example.com/MyWebService\"") http.setRequestHeader("Host", value: "www.example.com") http.setRequestHeader("Content-Type", value: "text/xml; charset=utf-8") var resp: CkoHttpResponse? = http.pTextSb("POST", url: "https://example.com/MyWebService", textData: sbXml, charset: "utf-8", contentType: "text/xml; charset=utf-8", md5: false, gzip: false) if http.lastMethodSuccess == false { print("\(http.lastErrorText!)") return } print("\(resp!.statusCode.intValue)") print("\(resp!.bodyStr!)") resp = nil print("Finished.") } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.