SQL Server中調用WebService的實例


1.Ole Automation Procedures 服務器配置選項

當啟用 OLE Automation Procedures 時,對 sp_OACreate 的調用將會啟動 OLE 共享執行環境。

可以使用 sp_configure 系統存儲過程來查看和更改 Ole Automation Procedures 選項的當前值。

sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO

2.編寫SQL代碼並執行

declare @ServiceUrl as varchar(1000) 
declare @UrlAddress varchar(500)

--WebService地址:以http開頭,結尾帶斜杠,例如'http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/' 
set @UrlAddress = 'http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/'

declare @FunName varchar(50)
--WebService中調用的方法名:例如'getMobileCodeInfo'
set @FunName = 'getMobileCodeInfo'   

--以下參數對應WebService中4個參數的[參數名]
declare @P1 varchar(800),@P2 varchar(100)
set @P1 = 'mobileCode'
set @P2 = 'userid'

declare @P1_Value varchar(100),@P2_Value varchar(100)
set @P1_Value = '13800138000'
set @P2_Value = ''

set @ServiceUrl = @UrlAddress + @FunName + '?' + @P1 + '=' + @P1_Value +'&' + @P2 + '=' + @P2_Value                       
                  
Declare @Object as Int
Declare @ResponseText as Varchar(8000)
                  
Exec sp_OACreate 'MSXML2.XMLHTTP', @Object OUT;
Exec sp_OAMethod @Object, 'open', NULL, 'get',@ServiceUrl,'false'
Exec sp_OAMethod @Object, 'send'
Exec sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT
     
Select @ResponseText     
Exec sp_OADestroy @Object
GO
View Code

需注意,返回結果是帶解析的XML編碼。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM