SQL Server Stored Procedure Examples

ChilkatHOMEAndroid™ASPVisual BasicVB.NETC#iOS (IPhone)Objective-CC++CMFCDelphiFoxProJavaPerl
PHP ExtensionPHP ActiveXPythonPowerShellRubySQL ServerVBScript

SQL Server
Stored Procedure Examples

Quick Start
Encryption
File Access
IMAP
POP3
SMTP
Email Object
DKIM / DomainKey
FTP
HTML Conversion
HTTP
MHT
MIME
NTLM
RSA
Diffie-Hellman
DSA
Socket
Spider
SSH Key
SSH
SSH Tunnel
SFTP
String
Tar
Upload
XML
XMP
Zip

Amazon S3
Bz2
CSV
FileAccess
Byte Array
RSS
Atom
Self-Extractor

SOAP Web Service (HTTP POST)

Demonstrates how to call PostXml to call a SOAP web service.

Download Chilkat XML ActiveX

Download Chilkat HTTP ActiveX

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    --  Any string unlocks the component for the 1st 30-days.
    EXEC sp_OAMethod @http, 'UnlockComponent', @success OUT, 'Anything for 30-day trial'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        RETURN
      END

    --  This example uses a live web service at:
    --  http://www.webservicex.net/uklocation.asmx
    --  It gets the UK town,Postcode and County by Postcode(First Section of Post Code)

    --  The SOAP XML will look like this:
    --  <?xml version="1.0" encoding="utf-8"?>
    --  <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    --    <soap:Body>
    --      <GetUKLocationByPostCode xmlns="http://www.webserviceX.NET">
    --        <PostCode>string</PostCode>
    --      </GetUKLocationByPostCode>
    --    </soap:Body>
    --  </soap:Envelope>

    --  For this example's sake, we'll load the XML from a file.  We'll test using PostCode "E3".
    --  The SOAP XML for this example may be downloaded from:
    --  http://www.chilkatsoft.com/testData/ukPostCodeSoap.xml
    DECLARE @xml int
    EXEC @hr = sp_OACreate 'Chilkat.Xml', @xml OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @xml, 'LoadXmlFile', @success OUT, 'ukPostCodeSoap.xml'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @xml, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        RETURN
      END

    DECLARE @strXml nvarchar(4000)

    EXEC sp_OAMethod @xml, 'GetXml', @strXml OUT

    DECLARE @resp int

    DECLARE @responseStatusCode int

    --  We'll need to add this in the HTTP header:
    --  SOAPAction: "http://www.webserviceX.NET/GetUKLocationByPostCode"
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'SOAPAction', 'http://www.webserviceX.NET/GetUKLocationByPostCode'

    --  Some services expect the content-type in the HTTP header to be "application/xml" while
    --  other expect text/xml.  The default sent by Chilkat is "application/xml", but this web service
    --  expects "text/xml".  Therefore, change the content-type:
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'text/xml; charset=utf-8'

    --  The endpoint for this soap service is:
    DECLARE @endPoint nvarchar(4000)

    SELECT @endPoint = 'http://www.webservicex.net/uklocation.asmx'

    --  Note: This particular web service was live and functioning at the time this example
    --  was written.  It is possible that as time progresses, this web service will no longer
    --  be availble...
    EXEC sp_OAMethod @http, 'PostXml', @resp OUT, @endPoint, @strXml, 'utf-8'
    IF @resp Is NULL 
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT

        PRINT @sTmp0


        PRINT 'LastHeader:'
        EXEC sp_OAGetProperty @http, 'LastHeader', @sTmp0 OUT

        PRINT @sTmp0
      END
    ELSE
      BEGIN

        EXEC sp_OAGetProperty @resp, 'StatusCode', @responseStatusCode OUT

        --  You may wish to verify that the responseStatusCode equals 200...


        PRINT 'Response Status Code: ' + STR(@responseStatusCode)

        --  You may examine the exact HTTP header sent with the POST like this:

        PRINT 'LastHeader:'
        EXEC sp_OAGetProperty @http, 'LastHeader', @sTmp0 OUT

        PRINT @sTmp0

        --  Examine the XML returned by the web service:

        PRINT 'XML Response:'
        DECLARE @xmlResp int
        EXEC @hr = sp_OACreate 'Chilkat.Xml', @xmlResp OUT
        IF @hr <> 0
        BEGIN
            PRINT 'Failed to create ActiveX component'
            RETURN
        END

        EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
        EXEC sp_OAMethod @xmlResp, 'LoadXml', @success OUT, @sTmp0
        --  Assume the LoadXml is successful...
        --  Get rid of the SOAP wrappings and get to the meat of this particular response.
        --  The TagContent method returns the content of the 1st node in the XML document
        --  having a specific tag:
        DECLARE @unwrappedXml nvarchar(4000)

        EXEC sp_OAMethod @xmlResp, 'TagContent', @unwrappedXml OUT, 'GetUKLocationByPostCodeResult'

        PRINT @unwrappedXml

        --  The unwrapped XML could be loaded into an XML object and parsed...
        DECLARE @xmlMeat int
        EXEC @hr = sp_OACreate 'Chilkat.Xml', @xmlMeat OUT
        IF @hr <> 0
        BEGIN
            PRINT 'Failed to create ActiveX component'
            RETURN
        END

        EXEC sp_OAMethod @xmlMeat, 'LoadXml', @success OUT, @unwrappedXml

        --  ...
      END


END
GO

 

© 2000-2010 Chilkat Software, Inc. All Rights Reserved.