Sqlserver調用api


雖然使用sqlserver去調用服務接口的情況比較少,但也可以去了解下對應的使用情況

一、首先要開啟組件的配置

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

 

二、調用webservice

1、接口信息獲取

調用webservice的時候建議使用fiddler去獲取一下發送數據過程用contenttype的類型以及調用接口的數據

2、使用sqlserver調用對應的接口以及結果

declare @ServiceUrl as varchar(1000) 
set @ServiceUrl = 'http://localhost:19930/LoginWebService.asmx/Login'
DECLARE @data varchar(max);
set @data='username=8&password=7'                  

Declare @Object as Int
Declare @ResponseText AS  varchar(1000)   ;      
Exec sp_OACreate 'Msxml2.ServerXMLHTTP.3.0', @Object OUT;
Exec sp_OAMethod @Object, 'open', NULL, 'POST',@ServiceUrl,'false'
Exec sp_OAMethod @Object, 'setRequestHeader', NULL, 'Content-Type','application/x-www-form-urlencoded'
Exec sp_OAMethod @Object, 'send', NULL, @data --發送數據
Exec sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT
EXEC sp_OAGetErrorInfo @Object --異常輸出
Select  @ResponseText 
Exec sp_OADestroy @Object
GO

三、調用webapi

 兩者調用的方式基本如出一轍

1、接口信息獲取

同樣使用fiddler獲取接口調用信息(因為該接口是GET就不需要看所傳的參數)

2、接口調用以及結果

GET操作

declare @ServiceUrl as varchar(1000) 
set @ServiceUrl = 'http://xxxxx.com/beijing/139/1000000/TaxInfo?token=6d83d2adcff64594bd68614b6ae9e1c8'
DECLARE @data varchar(max);
set @data=''                  

Declare @Object as Int
Declare @ResponseText AS  varchar(8000)   ;      
Exec sp_OACreate 'Msxml2.ServerXMLHTTP.3.0', @Object OUT;
Exec sp_OAMethod @Object, 'open', NULL, 'GET',@ServiceUrl,'false'
Exec sp_OAMethod @Object, 'send', NULL, @data --發送數據
Exec sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT
EXEC sp_OAGetErrorInfo @Object --異常輸出
Select  @ResponseText 
Exec sp_OADestroy @Object
GO

 

POST操作

declare @ServiceUrl as varchar(1000) 
set @ServiceUrl = 'http://xxxx.com/Feedback/Estimate'
DECLARE @data varchar(max);
--發送數據
set @data='CityName=SubmitSystemName=%E7%99%BE%E5%BA%A6%E5%8F%8D%E9%A6%88&OriginID=2d90660c-436c-4e12-bfa6-e849a06b2c51&Price=10000&IsAccurate=False&PriceType=1&UserKeyId=a669e4ec7bdc47a7b6c2c334ebe1a50c&signature=X8p3lIZT0Ba3LeiC6irm3%2FMnlE8%3D&time=1452735047291'                   

Declare @Object as Int
Declare @ResponseText AS  varchar(8000)   ;      
Exec sp_OACreate 'Msxml2.ServerXMLHTTP.3.0', @Object OUT;
Exec sp_OAMethod @Object, 'open', NULL, 'POST',@ServiceUrl,'false'
Exec sp_OAMethod @Object, 'setRequestHeader', NULL, 'Content-Type','application/x-www-form-urlencoded'
Exec sp_OAMethod @Object, 'send', NULL, @data --發送數據
Exec sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT
EXEC sp_OAGetErrorInfo @Object --異常輸出
Select  @ResponseText 
Exec sp_OADestroy @Object
GO

  

 


免責聲明!

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



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