Sample code for 30+ languages & platforms
Xojo Plugin

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 Xojo Plugin Downloads

Xojo Plugin
Dim success As Boolean
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
// ------------------------------------
Dim cert As New Chilkat.Cert
success = cert.LoadFromSmartcard("")
If (success = False) Then
    System.DebugLog(cert.LastErrorText)
    Return
End If

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

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

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

Dim gen As New Chilkat.XmlDSigGen

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 --------
success = gen.AddSameDocRef("TheBody","sha1","EXCL_C14N","wsu SOAP-ENV","")

success = gen.SetX509Cert(cert,True)

gen.KeyInfoType = "Custom"

// Create the custom KeyInfo XML..
Dim xmlCustomKeyInfo As New Chilkat.Xml
xmlCustomKeyInfo.Tag = "wsse:SecurityTokenReference"
xmlCustomKeyInfo.Content = "5"
success = xmlCustomKeyInfo.UpdateAttrAt("wsse:Reference",True,"URI","#x509cert00")
success = xmlCustomKeyInfo.UpdateAttrAt("wsse:Reference",True,"ValueType","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...
Dim sbXml As New Chilkat.StringBuilder
xml.EmitXmlDecl = False
success = xml.GetXmlSb(sbXml)

gen.Behaviors = "IndentedSignature"

// Sign the XML...
success = gen.CreateXmlDSigSb(sbXml)
If (success = False) Then
    System.DebugLog(gen.LastErrorText)
    Return
End If

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

Dim http As New Chilkat.Http

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

Dim resp As New Chilkat.HttpResponse
success = http.HttpSb("POST","https://example.com/MyWebService",sbXml,"utf-8","text/xml; charset=utf-8",resp)
If (success = False) Then
    System.DebugLog(http.LastErrorText)
    Return
End If

System.DebugLog(Str(resp.StatusCode))
System.DebugLog(resp.BodyStr)

System.DebugLog("Finished.")