Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Cert
oleobject loo_Xml
oleobject loo_Gen
oleobject loo_XmlCustomKeyInfo
oleobject loo_SbXml
oleobject loo_Http
oleobject loo_Resp

li_Success = 0

// 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
// ------------------------------------
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")
if li_rc < 0 then
    destroy loo_Cert
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Cert.LoadFromSmartcard("")
if li_Success = 0 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_Cert
    return
end if

// ---------------------------------------
// Step 2: Build the SOAP XML to be signed
// ---------------------------------------

loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")

loo_Xml.Tag = "SOAP-ENV:Envelope"
loo_Xml.AddAttribute("xmlns:SOAP-ENV","http://schemas.xmlsoap.org/soap/envelope/")
loo_Xml.AddAttribute("xmlns:web","http://www.example.com/webservice/")
loo_Xml.UpdateAttrAt("SOAP-ENV:Header|wsse:Security",1,"xmlns:ds","http://www.w3.org/2000/09/xmldsig#")
loo_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")
loo_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")
loo_Xml.UpdateAttrAt("SOAP-ENV:Header|wsse:Security",1,"xmlns:xenc","http://www.w3.org/2001/04/xmlenc#")
loo_Xml.UpdateAttrAt("SOAP-ENV:Header|wsse:Security",1,"SOAP-ENV:mustUnderstand","1")
loo_Xml.UpdateAttrAt("SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",1,"A1","")
loo_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")
loo_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")
loo_Xml.UpdateAttrAt("SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",1,"wsu:Id","x509cert00")
loo_Xml.UpdateChildContent("SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",loo_Cert.GetEncoded())
loo_Xml.UpdateAttrAt("SOAP-ENV:Body",1,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")
loo_Xml.UpdateAttrAt("SOAP-ENV:Body",1,"wsu:Id","TheBody")
loo_Xml.UpdateChildContent("SOAP-ENV:Body|web:MyRequest|web:Parameter","MyValue")

// ---------------------------------------
// Step 3: Sign the XML
// ---------------------------------------

loo_Gen = create oleobject
li_rc = loo_Gen.ConnectToNewObject("Chilkat.XmlDSigGen")

loo_Gen.SigLocation = "SOAP-ENV:Envelope|SOAP-ENV:Header|wsse:Security"
loo_Gen.SigLocationMod = 0
loo_Gen.SigNamespacePrefix = "ds"
loo_Gen.SigNamespaceUri = "http://www.w3.org/2000/09/xmldsig#"
loo_Gen.SignedInfoPrefixList = "ds wsu xenc SOAP-ENV "
loo_Gen.IncNamespacePrefix = "c14n"
loo_Gen.IncNamespaceUri = "http://www.w3.org/2001/10/xml-exc-c14n#"
loo_Gen.SignedInfoCanonAlg = "EXCL_C14N"
loo_Gen.SignedInfoDigestMethod = "sha1"

// -------- Reference 1 --------
loo_Gen.AddSameDocRef("TheBody","sha1","EXCL_C14N","wsu SOAP-ENV","")

loo_Gen.SetX509Cert(loo_Cert,1)

loo_Gen.KeyInfoType = "Custom"

// Create the custom KeyInfo XML..
loo_XmlCustomKeyInfo = create oleobject
li_rc = loo_XmlCustomKeyInfo.ConnectToNewObject("Chilkat.Xml")

loo_XmlCustomKeyInfo.Tag = "wsse:SecurityTokenReference"
loo_XmlCustomKeyInfo.Content = "5"
loo_XmlCustomKeyInfo.UpdateAttrAt("wsse:Reference",1,"URI","#x509cert00")
loo_XmlCustomKeyInfo.UpdateAttrAt("wsse:Reference",1,"ValueType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509")

loo_XmlCustomKeyInfo.EmitXmlDecl = 0
loo_Gen.CustomKeyInfoXml = loo_XmlCustomKeyInfo.GetXml()

// Load XML to be signed...
loo_SbXml = create oleobject
li_rc = loo_SbXml.ConnectToNewObject("Chilkat.StringBuilder")

loo_Xml.EmitXmlDecl = 0
loo_Xml.GetXmlSb(loo_SbXml)

loo_Gen.Behaviors = "IndentedSignature"

// Sign the XML...
li_Success = loo_Gen.CreateXmlDSigSb(loo_SbXml)
if li_Success = 0 then
    Write-Debug loo_Gen.LastErrorText
    destroy loo_Cert
    destroy loo_Xml
    destroy loo_Gen
    destroy loo_XmlCustomKeyInfo
    destroy loo_SbXml
    return
end if

// ---------------------------------------------------------
// Step 4: Send the HTTP POST containing the Signed SOAP XML
// ---------------------------------------------------------

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")

loo_Http.SetRequestHeader("SOAPAction","~"http://www.example.com/MyWebService~"")
loo_Http.SetRequestHeader("Host","www.example.com")
loo_Http.SetRequestHeader("Content-Type","text/xml; charset=utf-8")

loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Http.HttpSb("POST","https://example.com/MyWebService",loo_SbXml,"utf-8","text/xml; charset=utf-8",loo_Resp)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Cert
    destroy loo_Xml
    destroy loo_Gen
    destroy loo_XmlCustomKeyInfo
    destroy loo_SbXml
    destroy loo_Http
    destroy loo_Resp
    return
end if

Write-Debug string(loo_Resp.StatusCode)
Write-Debug loo_Resp.BodyStr

Write-Debug "Finished."


destroy loo_Cert
destroy loo_Xml
destroy loo_Gen
destroy loo_XmlCustomKeyInfo
destroy loo_SbXml
destroy loo_Http
destroy loo_Resp