PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkHttp.pb"
IncludeFile "CkXml.pb"
IncludeFile "CkXmlDSigGen.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkCert.pb"
Procedure ChilkatExample()
success.i = 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
; ------------------------------------
cert.i = CkCert::ckCreate()
If cert.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkCert::ckLoadFromSmartcard(cert,"")
If success = 0
Debug CkCert::ckLastErrorText(cert)
CkCert::ckDispose(cert)
ProcedureReturn
EndIf
; ---------------------------------------
; Step 2: Build the SOAP XML to be signed
; ---------------------------------------
xml.i = CkXml::ckCreate()
If xml.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkXml::setCkTag(xml, "SOAP-ENV:Envelope")
CkXml::ckAddAttribute(xml,"xmlns:SOAP-ENV","http://schemas.xmlsoap.org/soap/envelope/")
CkXml::ckAddAttribute(xml,"xmlns:web","http://www.example.com/webservice/")
CkXml::ckUpdateAttrAt(xml,"SOAP-ENV:Header|wsse:Security",1,"xmlns:ds","http://www.w3.org/2000/09/xmldsig#")
CkXml::ckUpdateAttrAt(xml,"SOAP-ENV:Header|wsse:Security",1,"xmlns:wsse","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")
CkXml::ckUpdateAttrAt(xml,"SOAP-ENV:Header|wsse:Security",1,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")
CkXml::ckUpdateAttrAt(xml,"SOAP-ENV:Header|wsse:Security",1,"xmlns:xenc","http://www.w3.org/2001/04/xmlenc#")
CkXml::ckUpdateAttrAt(xml,"SOAP-ENV:Header|wsse:Security",1,"SOAP-ENV:mustUnderstand","1")
CkXml::ckUpdateAttrAt(xml,"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",1,"A1","")
CkXml::ckUpdateAttrAt(xml,"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")
CkXml::ckUpdateAttrAt(xml,"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")
CkXml::ckUpdateAttrAt(xml,"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",1,"wsu:Id","x509cert00")
CkXml::ckUpdateChildContent(xml,"SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken",CkCert::ckGetEncoded(cert))
CkXml::ckUpdateAttrAt(xml,"SOAP-ENV:Body",1,"xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")
CkXml::ckUpdateAttrAt(xml,"SOAP-ENV:Body",1,"wsu:Id","TheBody")
CkXml::ckUpdateChildContent(xml,"SOAP-ENV:Body|web:MyRequest|web:Parameter","MyValue")
; ---------------------------------------
; Step 3: Sign the XML
; ---------------------------------------
gen.i = CkXmlDSigGen::ckCreate()
If gen.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkXmlDSigGen::setCkSigLocation(gen, "SOAP-ENV:Envelope|SOAP-ENV:Header|wsse:Security")
CkXmlDSigGen::setCkSigLocationMod(gen, 0)
CkXmlDSigGen::setCkSigNamespacePrefix(gen, "ds")
CkXmlDSigGen::setCkSigNamespaceUri(gen, "http://www.w3.org/2000/09/xmldsig#")
CkXmlDSigGen::setCkSignedInfoPrefixList(gen, "ds wsu xenc SOAP-ENV ")
CkXmlDSigGen::setCkIncNamespacePrefix(gen, "c14n")
CkXmlDSigGen::setCkIncNamespaceUri(gen, "http://www.w3.org/2001/10/xml-exc-c14n#")
CkXmlDSigGen::setCkSignedInfoCanonAlg(gen, "EXCL_C14N")
CkXmlDSigGen::setCkSignedInfoDigestMethod(gen, "sha1")
; -------- Reference 1 --------
CkXmlDSigGen::ckAddSameDocRef(gen,"TheBody","sha1","EXCL_C14N","wsu SOAP-ENV","")
CkXmlDSigGen::ckSetX509Cert(gen,cert,1)
CkXmlDSigGen::setCkKeyInfoType(gen, "Custom")
; Create the custom KeyInfo XML..
xmlCustomKeyInfo.i = CkXml::ckCreate()
If xmlCustomKeyInfo.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkXml::setCkTag(xmlCustomKeyInfo, "wsse:SecurityTokenReference")
CkXml::setCkContent(xmlCustomKeyInfo, "5")
CkXml::ckUpdateAttrAt(xmlCustomKeyInfo,"wsse:Reference",1,"URI","#x509cert00")
CkXml::ckUpdateAttrAt(xmlCustomKeyInfo,"wsse:Reference",1,"ValueType","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509")
CkXml::setCkEmitXmlDecl(xmlCustomKeyInfo, 0)
CkXmlDSigGen::setCkCustomKeyInfoXml(gen, CkXml::ckGetXml(xmlCustomKeyInfo))
; Load XML to be signed...
sbXml.i = CkStringBuilder::ckCreate()
If sbXml.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkXml::setCkEmitXmlDecl(xml, 0)
CkXml::ckGetXmlSb(xml,sbXml)
CkXmlDSigGen::setCkBehaviors(gen, "IndentedSignature")
; Sign the XML...
success = CkXmlDSigGen::ckCreateXmlDSigSb(gen,sbXml)
If success = 0
Debug CkXmlDSigGen::ckLastErrorText(gen)
CkCert::ckDispose(cert)
CkXml::ckDispose(xml)
CkXmlDSigGen::ckDispose(gen)
CkXml::ckDispose(xmlCustomKeyInfo)
CkStringBuilder::ckDispose(sbXml)
ProcedureReturn
EndIf
; ---------------------------------------------------------
; Step 4: Send the HTTP POST containing the Signed SOAP XML
; ---------------------------------------------------------
http.i = CkHttp::ckCreate()
If http.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttp::ckSetRequestHeader(http,"SOAPAction",Chr(34) + "http://www.example.com/MyWebService" + Chr(34))
CkHttp::ckSetRequestHeader(http,"Host","www.example.com")
CkHttp::ckSetRequestHeader(http,"Content-Type","text/xml; charset=utf-8")
resp.i = CkHttpResponse::ckCreate()
If resp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkHttp::ckHttpSb(http,"POST","https://example.com/MyWebService",sbXml,"utf-8","text/xml; charset=utf-8",resp)
If success = 0
Debug CkHttp::ckLastErrorText(http)
CkCert::ckDispose(cert)
CkXml::ckDispose(xml)
CkXmlDSigGen::ckDispose(gen)
CkXml::ckDispose(xmlCustomKeyInfo)
CkStringBuilder::ckDispose(sbXml)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
ProcedureReturn
EndIf
Debug Str(CkHttpResponse::ckStatusCode(resp))
Debug CkHttpResponse::ckBodyStr(resp)
Debug "Finished."
CkCert::ckDispose(cert)
CkXml::ckDispose(xml)
CkXmlDSigGen::ckDispose(gen)
CkXml::ckDispose(xmlCustomKeyInfo)
CkStringBuilder::ckDispose(sbXml)
CkHttp::ckDispose(http)
CkHttpResponse::ckDispose(resp)
ProcedureReturn
EndProcedure