Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Variant vCert
    Handle hoCert
    Handle hoXml
    Handle hoGen
CustomKeyInfo    Handle hoXmlCustomKeyInfo
    Variant vSbXml
    Handle hoSbXml
    Handle hoHttp
    Variant vResp
    Handle hoResp
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

    // 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
    // ------------------------------------
    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End
    Get ComLoadFromSmartcard Of hoCert "" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCert To sTemp1
        Showln sTemp1
        Procedure_Return
    End

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

    Get Create (RefClass(cComChilkatXml)) To hoXml
    If (Not(IsComObjectCreated(hoXml))) Begin
        Send CreateComObject of hoXml
    End
    Set ComTag Of hoXml To "SOAP-ENV:Envelope"
    Get ComAddAttribute Of hoXml "xmlns:SOAP-ENV" "http://schemas.xmlsoap.org/soap/envelope/" To iSuccess
    Get ComAddAttribute Of hoXml "xmlns:web" "http://www.example.com/webservice/" To iSuccess
    Get ComUpdateAttrAt Of hoXml "SOAP-ENV:Header|wsse:Security" True "xmlns:ds" "http://www.w3.org/2000/09/xmldsig#" To iSuccess
    Get ComUpdateAttrAt Of hoXml "SOAP-ENV:Header|wsse:Security" True "xmlns:wsse" "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" To iSuccess
    Get ComUpdateAttrAt Of hoXml "SOAP-ENV:Header|wsse:Security" True "xmlns:wsu" "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" To iSuccess
    Get ComUpdateAttrAt Of hoXml "SOAP-ENV:Header|wsse:Security" True "xmlns:xenc" "http://www.w3.org/2001/04/xmlenc#" To iSuccess
    Get ComUpdateAttrAt Of hoXml "SOAP-ENV:Header|wsse:Security" True "SOAP-ENV:mustUnderstand" "1" To iSuccess
    Get ComUpdateAttrAt Of hoXml "SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken" True "A1" "" To iSuccess
    Get ComUpdateAttrAt Of hoXml "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" To iSuccess
    Get ComUpdateAttrAt Of hoXml "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" To iSuccess
    Get ComUpdateAttrAt Of hoXml "SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken" True "wsu:Id" "x509cert00" To iSuccess
    Get ComGetEncoded Of hoCert To sTemp1
    Send ComUpdateChildContent To hoXml "SOAP-ENV:Header|wsse:Security|wsse:BinarySecurityToken" sTemp1
    Get ComUpdateAttrAt Of hoXml "SOAP-ENV:Body" True "xmlns:wsu" "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" To iSuccess
    Get ComUpdateAttrAt Of hoXml "SOAP-ENV:Body" True "wsu:Id" "TheBody" To iSuccess
    Send ComUpdateChildContent To hoXml "SOAP-ENV:Body|web:MyRequest|web:Parameter" "MyValue"

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

    Get Create (RefClass(cComChilkatXmlDSigGen)) To hoGen
    If (Not(IsComObjectCreated(hoGen))) Begin
        Send CreateComObject of hoGen
    End

    Set ComSigLocation Of hoGen To "SOAP-ENV:Envelope|SOAP-ENV:Header|wsse:Security"
    Set ComSigLocationMod Of hoGen To 0
    Set ComSigNamespacePrefix Of hoGen To "ds"
    Set ComSigNamespaceUri Of hoGen To "http://www.w3.org/2000/09/xmldsig#"
    Set ComSignedInfoPrefixList Of hoGen To "ds wsu xenc SOAP-ENV "
    Set ComIncNamespacePrefix Of hoGen To "c14n"
    Set ComIncNamespaceUri Of hoGen To "http://www.w3.org/2001/10/xml-exc-c14n#"
    Set ComSignedInfoCanonAlg Of hoGen To "EXCL_C14N"
    Set ComSignedInfoDigestMethod Of hoGen To "sha1"

    // -------- Reference 1 --------
    Get ComAddSameDocRef Of hoGen "TheBody" "sha1" "EXCL_C14N" "wsu SOAP-ENV" "" To iSuccess

    Get pvComObject of hoCert to vCert
    Get ComSetX509Cert Of hoGen vCert True To iSuccess

    Set ComKeyInfoType Of hoGen To "Custom"

    // Create the custom KeyInfo XML..
    Get Create (RefClass(cComChilkatXml)) To hoXmlCustomKeyInfo
    If (Not(IsComObjectCreated(hoXmlCustomKeyInfo))) Begin
        Send CreateComObject of hoXmlCustomKeyInfo
    End
    Set ComTag Of hoXmlCustomKeyInfo To "wsse:SecurityTokenReference"
    Set ComContent Of hoXmlCustomKeyInfo To "5"
    Get ComUpdateAttrAt Of hoXmlCustomKeyInfo "wsse:Reference" True "URI" "#x509cert00" To iSuccess
    Get ComUpdateAttrAt Of hoXmlCustomKeyInfo "wsse:Reference" True "ValueType" "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509" To iSuccess

    Set ComEmitXmlDecl Of hoXmlCustomKeyInfo To False
    Get ComGetXml Of hoXmlCustomKeyInfo To sTemp1
    Set ComCustomKeyInfoXml Of hoGen To sTemp1

    // Load XML to be signed...
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbXml
    If (Not(IsComObjectCreated(hoSbXml))) Begin
        Send CreateComObject of hoSbXml
    End
    Set ComEmitXmlDecl Of hoXml To False
    Get pvComObject of hoSbXml to vSbXml
    Get ComGetXmlSb Of hoXml vSbXml To iSuccess

    Set ComBehaviors Of hoGen To "IndentedSignature"

    // Sign the XML...
    Get pvComObject of hoSbXml to vSbXml
    Get ComCreateXmlDSigSb Of hoGen vSbXml To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoGen To sTemp1
        Showln sTemp1
        Procedure_Return
    End

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

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    Send ComSetRequestHeader To hoHttp "SOAPAction" '"http://www.example.com/MyWebService"'
    Send ComSetRequestHeader To hoHttp "Host" "www.example.com"
    Send ComSetRequestHeader To hoHttp "Content-Type" "text/xml; charset=utf-8"

    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End
    Get pvComObject of hoSbXml to vSbXml
    Get pvComObject of hoResp to vResp
    Get ComHttpSb Of hoHttp "POST" "https://example.com/MyWebService" vSbXml "utf-8" "text/xml; charset=utf-8" vResp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStatusCode Of hoResp To iTemp1
    Showln iTemp1
    Get ComBodyStr Of hoResp To sTemp1
    Showln sTemp1

    Showln "Finished."


End_Procedure