23 Mart 2018

SQL ile Web Servis Kullanmak

http://secure.smartbearsoftware.com/samples/testcomplete12/webservices/Service.asmx adresi üzerindeki demo web servisten HelloWorld metodunu çağıracağız. Örnek kod aşağıdadır.

DECLARE @Object INT;
DECLARE @Status INT;
DECLARE @Response NVARCHAR(1000);
 
 
DECLARE @requestBody NVARCHAR(MAX) = '<?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>
    <HelloWorld xmlns="http://smartbear.com" />
  </soap:Body>
</soap:Envelope>'
 
 
EXEC sp_OACreate 'WinHttp.WinHttpRequest.5.1', @Object OUT;
EXEC sp_OAMethod @Object, 'Open', NULL, 'POST',  'http://secure.smartbearsoftware.com/samples/testcomplete12/webservices/Service.asmx', 'false'
EXEC sp_OAMethod @Object, 'setRequestHeader', null, 'Host', 'secure.smartbearsoftware.com'
EXEC sp_OAMethod @Object, 'setRequestHeader', null, 'Content-Type', 'text/xml'
DECLARE @len INT = len(@requestBody) 
EXEC sp_OAMethod @Object, 'setRequestHeader', null, 'Content-Length', @len
EXEC sp_OAMethod @Object, 'setRequestHeader', null, 'SOAPAction', "http://smartbear.com/HelloWorld"
EXEC sp_OAMethod @Object, 'send', null, @requestBody
 
EXEC sp_OAGetProperty @Object, 'Status', @Status OUT
EXEC sp_OAGetProperty @Object, 'ResponseText', @Response OUTPUT
EXEC sp_OADestroy @Object
 
PRINT @Status
PRINT @Response