SQL Server
SQL Server
Yet Another SOAP MTOM POST Example
See more HTTP Misc Examples
Demonstrates how to sending a SOAP request with an MTOM/XOP attachment...Chilkat SQL Server Downloads
-- 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 requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- First build the following XML:
-- <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
-- xmlns:imp="http://www.yourdomain.it/ImportMultiploWorkflowPAWS/">
-- <soapenv:Header/>
-- <soapenv:Body>
-- <imp:InputImportMultiploWorkflowPA>
-- <usernameEis>MyUsername</usernameEis>
-- <passwordEis>MyPassword</passwordEis>
-- <postazioneEis>COSO00</postazioneEis>
-- <istitutoEis>COSO</istitutoEis>
-- <codiceAzienda>COSO0000</codiceAzienda>
-- <fileZip><inc:Include href="cid:780931946797" xmlns:inc="http://www.w3.org/2004/08/xop/include"/></fileZip>
-- <fileZipName>IT04769180151_00044.zip</fileZipName>
-- <workflow>carica</workflow>
-- </imp:InputImportMultiploWorkflowPA>
-- </soapenv:Body>
-- </soapenv:Envelope>
DECLARE @xml int
EXEC @hr = sp_OACreate 'Chilkat.Xml', @xml OUT
EXEC sp_OASetProperty @xml, 'Tag', 'soapenv:Envelope'
EXEC sp_OAMethod @xml, 'AddAttribute', @success OUT, 'xmlns:soapenv', 'http://schemas.xmlsoap.org/soap/envelope/'
EXEC sp_OAMethod @xml, 'AddAttribute', @success OUT, 'xmlns:imp', 'http://www.yourdomain.it/ImportMultiploWorkflowPAWS/'
EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'soapenv:Header', ''
EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'soapenv:Body|imp:InputImportMultiploWorkflowPA|usernameEis', 'MyUsername'
EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'soapenv:Body|imp:InputImportMultiploWorkflowPA|passwordEis', 'MyPassword'
EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'soapenv:Body|imp:InputImportMultiploWorkflowPA|postazioneEis', 'COSO00'
EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'soapenv:Body|imp:InputImportMultiploWorkflowPA|istitutoEis', 'COSO'
EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'soapenv:Body|imp:InputImportMultiploWorkflowPA|codiceAzienda', 'COSO0000'
EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'soapenv:Body|imp:InputImportMultiploWorkflowPA|fileZip|inc:Include', 1, 'href', 'cid:780931946797'
EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'soapenv:Body|imp:InputImportMultiploWorkflowPA|fileZip|inc:Include', 1, 'xmlns:inc', 'http://www.w3.org/2004/08/xop/include'
EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'soapenv:Body|imp:InputImportMultiploWorkflowPA|fileZipName', 'IT04769180151_00044.zip'
EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'soapenv:Body|imp:InputImportMultiploWorkflowPA|workflow', 'carica'
-- We want our top-level HTTP request header to look like this:
-- POST /http-router-softwarehubsystem/webservices/importMultiploWorkflowPA HTTP/1.1
-- Accept-Encoding: gzip,deflate
-- Content-Type: multipart/related; type="application/xop+xml"; start="<rootpart@soapui.org>"; start-info="text/xml"; boundary="----=_Part_0_1353857996.1546013984933"
-- SOAPAction: "http://www.yourdomain.it/ImportMultiploWorkflowPAWS/importMultiploWorkflowPA"
-- MIME-Version: 1.0
-- Content-Length: <chilkat will add this header>
-- Host: <chilkat will add this header>
-- Connection: Keep-Alive
-- User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
DECLARE @req int
EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @req OUT
EXEC sp_OASetProperty @req, 'HttpVerb', 'POST'
EXEC sp_OASetProperty @req, 'Path', '/http-router-softwarehubsystem/webservices/importMultiploWorkflowPA'
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', '"http://www.yourdomain.it/ImportMultiploWorkflowPAWS/importMultiploWorkflowPA"'
EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Accept-Encoding', 'gzip,deflate'
EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Mime-Version', '1.0'
EXEC sp_OAMethod @req, 'AddHeader', NULL, 'Connection', 'Keep-Alive'
EXEC sp_OAMethod @req, 'AddHeader', NULL, 'User-Agent', 'Apache-HttpClient/4.1.1 (java 1.5)'
-- Add the XML content for the 1st MIME sub-part.
EXEC sp_OASetProperty @xml, 'EmitXmlDecl', 0
EXEC sp_OASetProperty @xml, 'EmitCompact', 0
EXEC sp_OAMethod @xml, 'GetXml', @sTmp0 OUT
EXEC sp_OAMethod @req, 'AddStringForUpload2', @success OUT, '', '', @sTmp0, 'utf-8', 'application/xop+xml; charset=UTF-8; type="text/xml"'
-- We want the 1st sub-header (the sub-part containing the XML) to look like this:
-- Content-Type: application/xop+xml; charset=UTF-8; type="text/xml"
-- Content-Transfer-Encoding: 8bit
-- Content-ID: <rootpart@soapui.org>
-- Make sure we set sub-headers *after* calling AddStringForUpload2, otherwise AddStringForUpload2 might change these explicitly set headers.
EXEC sp_OAMethod @req, 'AddSubHeader', @success OUT, 0, 'Content-Transfer-Encoding', '8bit'
EXEC sp_OAMethod @req, 'AddSubHeader', @success OUT, 0, 'Content-ID', '<rootpart@soapui.org>'
-- Add a reference to the local file for the file data for the 2nd MIME sub-part.
EXEC sp_OAMethod @req, 'AddFileForUpload2', @success OUT, '', 'qa_data/zips/helloWorld.zip', 'application/zip; name=IT04769180151_00044.zip'
-- We want the 2nd sub-header (for the MIME sub-part containing a .zip file) to look like this:
-- Content-Type: application/zip; name=IT04769180151_00044.zip
-- Content-Transfer-Encoding: binary
-- Content-ID: <IT04769180151_00044.zip>
-- Content-Disposition: attachment; name="IT04769180151_00044.zip"; filename="IT04769180151_00044.zip"
-- Make sure we set sub-headers *after* calling AddFileForUpload2, otherwise AddFileForUpload2 might change these explicitly set headers.
EXEC sp_OAMethod @req, 'AddSubHeader', @success OUT, 1, 'Content-Transfer-Encoding', 'binary'
EXEC sp_OAMethod @req, 'AddSubHeader', @success OUT, 1, 'Content-ID', '<IT04769180151_00044.zip>'
EXEC sp_OAMethod @req, 'AddSubHeader', @success OUT, 1, 'Content-Disposition', 'attachment; name="IT04769180151_00044.zip"; filename="IT04769180151_00044.zip"'
-- Keep a session log so we can examine the exact request/response.
-- Remove this line when finished with debugging.
EXEC sp_OASetProperty @http, 'SessionLogFilename', 'qa_output/mtom_sessionLog.txt'
DECLARE @useTls int
SELECT @useTls = 0
-- Note: Please don't run this example without changing the domain to your own domain...
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
EXEC sp_OAMethod @http, 'HttpSReq', @success OUT, 'www.yourdomain.it', 80, @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 @xml
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
-- println xmlResponse.GetXml();
PRINT 'OK.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @xml
EXEC @hr = sp_OADestroy @req
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @xmlResponse
END
GO