Calling a SOAP webservice from TSQL stored procedure

前端 未结 4 452
后悔当初
后悔当初 2021-01-02 15:52

I am trying to build a stored procedure in TSQL to call a webservice. I\'ve done this before in Oracle, but it seems like it\'s not so easy in MSSQL. There are of course man

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-02 16:25

    In the past I have used the following method, it may not be the best method these days but it has worked successfully for me :

    DECLARE @obj int,
            @url VarChar(MAX),
            @response VarChar(MAX),
            @requestHeader VarChar(MAX),
            @requestBody VarChar(MAX)
    
    SET @url = 'http://....'
    
    SET @requestBody = '
                         
                          
                           ...
                          
                         '
    
    EXEC sp_OACreate 'MSXML2.ServerXMLHttp', @obj OUT
    EXEC sp_OAMethod @obj, 'Open', NULL, 'GET', @url, false
    EXEC sp_OAMethod @obj, 'setRequestHeader', NULL, 'Content-Type', 'text/xml;charset=UTF-8'
    EXEC sp_OAMethod @obj, 'setRequestHeader', NULL, 'SOAPAction', 'POST'
    EXEC sp_OAMethod @obj, 'setRequestHeader', NULL, 'Content-Length', LEN(@requestBody)
    EXEC sp_OAMethod @obj, 'send', NULL, @requestBody
    EXEC sp_OAGetProperty @obj, 'responseText', @response OUT
    
    
    SELECT @response [RESPONSE]
    
    EXEC sp_OADestroy @obj
    

    I have used this to call a webservice which produces a report and emails it within the method.

提交回复
热议问题