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
(Perl) 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.
use chilkat(); # 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 = chilkat::CkCert->new(); $success = $cert->LoadFromSmartcard(""); if ($success == 0) { print $cert->lastErrorText() . "\r\n"; exit; } # --------------------------------------- # Step 2: Build the SOAP XML to be signed # --------------------------------------- $xml = chilkat::CkXml->new(); $xml->put_Tag("SOAP-ENV:Envelope"); $xml->AddAttribute("xmlns:SOAP-ENV","http://schemas.xmlsoap.org/soap/envelope/"); $xml->AddAttribute("xmlns:web","http://www.example.com/webservice/"); $xml->UpdateAttrAt("SOAP-ENV:Header|wsse:Security",1,"xmlns:ds","http://www.w3.org/2000/09/xmldsig#"); $xml->UpdateAttrAt("SOAP-ENV:Header|wsse:Security",1,"xmlns:wsse","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"); $xml->UpdateAttrAt("SOAP-ENV:Header|wsse:Security",1,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"); $xml->UpdateAttrAt("SOAP-ENV:Header|wsse:Security",1,"xmlns:xenc","http://www.w3.org/2001/04/xmlenc#"); $xml->UpdateAttrAt("SOAP-ENV:Header|wsse:Security",1,"SOAP-ENV:mustUnderstand","1"); $xml->UpdateAttrAt("SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",1,"A1",""); $xml->UpdateAttrAt("SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",1,"EncodingType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"); $xml->UpdateAttrAt("SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",1,"ValueType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509"); $xml->UpdateAttrAt("SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",1,"wsu:Id","x509cert00"); $xml->UpdateChildContent("SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",$cert->getEncoded()); $xml->UpdateAttrAt("SOAP-ENV:Body",1,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"); $xml->UpdateAttrAt("SOAP-ENV:Body",1,"wsu:Id","TheBody"); $xml->UpdateChildContent("SOAP-ENV:Body|web:MyRequest|web:Parameter","MyValue"); # --------------------------------------- # Step 3: Sign the XML # --------------------------------------- $gen = chilkat::CkXmlDSigGen->new(); $gen->put_SigLocation("SOAP-ENV:Envelope|SOAP-ENV:Header|wsse:Security"); $gen->put_SigLocationMod(0); $gen->put_SigNamespacePrefix("ds"); $gen->put_SigNamespaceUri("http://www.w3.org/2000/09/xmldsig#"); $gen->put_SignedInfoPrefixList("ds wsu xenc SOAP-ENV "); $gen->put_IncNamespacePrefix("c14n"); $gen->put_IncNamespaceUri("http://www.w3.org/2001/10/xml-exc-c14n#"); $gen->put_SignedInfoCanonAlg("EXCL_C14N"); $gen->put_SignedInfoDigestMethod("sha1"); # -------- Reference 1 -------- $gen->AddSameDocRef("TheBody","sha1","EXCL_C14N","wsu SOAP-ENV",""); $gen->SetX509Cert($cert,1); $gen->put_KeyInfoType("Custom"); # Create the custom KeyInfo XML.. $xmlCustomKeyInfo = chilkat::CkXml->new(); $xmlCustomKeyInfo->put_Tag("wsse:SecurityTokenReference"); $xmlCustomKeyInfo->put_Content("5"); $xmlCustomKeyInfo->UpdateAttrAt("wsse:Reference",1,"URI","#x509cert00"); $xmlCustomKeyInfo->UpdateAttrAt("wsse:Reference",1,"ValueType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509"); $xmlCustomKeyInfo->put_EmitXmlDecl(0); $gen->put_CustomKeyInfoXml($xmlCustomKeyInfo->getXml()); # Load XML to be signed... $sbXml = chilkat::CkStringBuilder->new(); $xml->put_EmitXmlDecl(0); $xml->GetXmlSb($sbXml); $gen->put_Behaviors("IndentedSignature"); # Sign the XML... $success = $gen->CreateXmlDSigSb($sbXml); if ($success != 1) { print $gen->lastErrorText() . "\r\n"; exit; } # --------------------------------------------------------- # Step 4: Send the HTTP POST containing the Signed SOAP XML # --------------------------------------------------------- $http = chilkat::CkHttp->new(); $http->SetRequestHeader("SOAPAction","\"http://www.example.com/MyWebService\""); $http->SetRequestHeader("Host","www.example.com"); $http->SetRequestHeader("Content-Type","text/xml; charset=utf-8"); # resp is a HttpResponse $resp = $http->PTextSb("POST","https://example.com/MyWebService",$sbXml,"utf-8","text/xml; charset=utf-8",0,0); if ($http->get_LastMethodSuccess() == 0) { print $http->lastErrorText() . "\r\n"; exit; } print $resp->get_StatusCode() . "\r\n"; print $resp->bodyStr() . "\r\n"; print "Finished." . "\r\n"; |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.