Sample code for 30+ languages & platforms
SQL Server

Another SOAP with MTOM XOP Attachment Example

See more HTTP Examples

Demonstrates another multipart/related MTOM SOAP request that adds additional headers in each MIME sub-part, and also specifies Content-Transfer-Encoding in the sub-parts.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- This example sends a request such as the following:

    -- POST /FseInsServicesWeb/services/fseComunicazioneMetadati HTTP/1.1
    -- Connection: Keep-Alive
    -- Content-Length: 50649
    -- Content-Type: multipart/related; type="application/xop+xml"; start="<rootpart@soapui.org>"; start-info="text/xml"; boundary="----=_Part_0_355796458.1662133302632"
    -- Accept-Encoding: gzip,deflate
    -- Authorization: Basic xxxxx
    -- Host: ...
    -- SOAPAction: "http://comunicazionemetadati.wsdl.fse.ini.finanze.it/ComunicazioneMetadati"
    -- MIME-Version: 1.0
    -- 
    -- ------=_Part_0_355796458.1662133302632
    -- Content-Type: application/xop+xml; charset=UTF-8; type="text/xml"
    -- Content-Transfer-Encoding: 8bit
    -- Content-ID: <rootpart@soapui.org>
    -- 
    -- <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:com="http://comunicazionemetadatirichiesta.xsd.fse.ini.finanze.it" xmlns:tip="http://tipodaticomunicazionemetadati.xsd.fse.ini.finanze.it">
    -- <soapenv:Header/>
    -- ...
    -- 
    -- ------=_Part_0_355796458.1662133302632
    -- Content-Type: text/xml; charset=Cp1252
    -- Content-Transfer-Encoding: quoted-printable
    -- Content-ID: <CDA2LAB_190_signed.xml>
    -- Content-Disposition: attachment; name="CDA2LAB_190_signed.xml"
    -- 
    -- <!-- INSERIRE IL RIFERIMENTO AL FOGLIO DI STILE DI AGID OPPURE INSERIRNE UN=
    -- ...
    -- 
    -- ------=_Part_0_355796458.1662133302632--

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

    DECLARE @req int
    EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @req OUT

    EXEC sp_OASetProperty @req, 'HttpVerb', 'POST'
    EXEC sp_OASetProperty @req, 'Path', '/FseInsServicesWeb/services/fseComunicazioneMetadati'

    -- Chilkat will automatically generate and add a boundary string.
    EXEC sp_OASetProperty @req, 'ContentType', 'multipart/related; type="application/xop+xml"; start="<rootpart@soapui.org>"; start-info="text/xml"'
    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'SOAPAction', 'some-SOAP-action'

    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Connection', 'Keep-Alive'
    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Accept-Encoding', 'gzip,deflate'
    EXEC sp_OAMethod @req, 'AddHeader', NULL, 'MIME-Version', '1.0'

    -- Chilkat will automatically add the Content-Length and Host headers.

    -- The "Authorization: Basic ..."  header is added by setting the Login and Password and specifying Basic authentication:
    EXEC sp_OASetProperty @http, 'Login', '...'
    EXEC sp_OASetProperty @http, 'Password', '...'
    EXEC sp_OASetProperty @http, 'BasicAuth', 1

    -- Add the 1st multipart/related sub-part, which is the SOAP envelope.
    DECLARE @xmlBody nvarchar(4000)
    SELECT @xmlBody = '<soapenv:Envelope ....'
    EXEC sp_OAMethod @req, 'AddStringForUpload2', @success OUT, '', '', @xmlBody, 'utf-8', 'application/xop+xml; type="text/xml"; charset=utf-8'

    -- Additional headers for the 1st sub-part
    EXEC sp_OAMethod @req, 'AddSubHeader', @success OUT, 0, 'Content-ID', '<rootpart@soapui.org>'
    EXEC sp_OAMethod @req, 'AddSubHeader', @success OUT, 0, 'Content-Transfer-Encoding', '8bit'
    EXEC sp_OAMethod @req, 'AddSubHeader', @success OUT, 0, 'Content-Disposition', ''

    -- Add the 2nd multipart/related sub-part, which is the signed XML
    DECLARE @xmlBody2 nvarchar(4000)
    SELECT @xmlBody2 = '<!-- INSERIRE IL RIFERIMENT ....'
    EXEC sp_OAMethod @req, 'AddStringForUpload2', @success OUT, 'CDA2LAB_190_signed.xml', '', @xmlBody2, 'Cp1252', 'text/xml; charset=Cp1252'

    -- Additional headers for the 2nd sub-part.
    EXEC sp_OAMethod @req, 'AddSubHeader', @success OUT, 1, 'Content-ID', '<CDA2LAB_190_signed.xml>'
    EXEC sp_OAMethod @req, 'AddSubHeader', @success OUT, 1, 'Content-Transfer-Encoding', 'quoted-printable'
    EXEC sp_OAMethod @req, 'AddSubHeader', @success OUT, 1, 'Content-Disposition', 'attachment; name="CDA2LAB_190_signed.xml"'

    EXEC sp_OASetProperty @http, 'FollowRedirects', 1

    DECLARE @useTls int
    SELECT @useTls = 1
    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    EXEC sp_OAMethod @http, 'HttpSReq', @success OUT, 'fseservicetest.sanita.finanze.it', 443, @useTls, @req, @resp
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @req
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END

    DECLARE @xmlResponse int
    EXEC @hr = sp_OACreate 'Chilkat.Xml', @xmlResponse OUT

    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    EXEC sp_OAMethod @xmlResponse, 'LoadXml', @success OUT, @sTmp0
    EXEC sp_OAMethod @xmlResponse, 'GetXml', @sTmp0 OUT
    PRINT @sTmp0

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @req
    EXEC @hr = sp_OADestroy @resp
    EXEC @hr = sp_OADestroy @xmlResponse


END
GO