虽然使用sqlserver去调用服务接口的情况比较少,但也可以去了解下对应的使用情况
一、首先要开启组件的配置
按 Ctrl+C 复制代码
按 Ctrl+C 复制代码
二、调用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操作
按 Ctrl+C 复制代码
按 Ctrl+C 复制代码
示例:
declare @ServiceUrl as varchar(1000)
set @ServiceUrl = 'http://36.33.216.237:8081/c/dict/getAllInstitutions' --restful api 接口
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, 'POST',@ServiceUrl,'false'
Exec sp_OAMethod @Object, 'setRequestHeader', NULL, 'Content-Type','application/json;charset=UTF-8'
Exec sp_OAMethod @Object, 'send', NULL, @data --发送数据
Exec sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT
--EXEC sp_OAGetErrorInfo @Object --异常输出
Select @ResponseText as Response
Exec sp_OADestroy @Object
GO