SQL Server定时调用指定WebApi接口

SQL Server定时调用指定WebApi接口 

JOB of Steps:

DECLARE @ApiUrl VARCHAR(2000)
DECLARE @RequestType VARCHAR(5)
DECLARE @ResponseText NVARCHAR(4000)
SET @ApiUrl = 'http://192.168.1.169/sit-qualityapi/QC_ExceptionRecord/SendOAMessage_ExceptionRecord'
SET @RequestType = 'POST'
EXEC Proc_CallWebApi @ApiUrl,@RequestType, @ResponseText OUTPUT

Proc_CallWebApi 

 
 
/****** Object:  StoredProcedure [dbo].[Proc_CallWebApi]    Script Date: 1/8/2024 2:36:11 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Proc_CallWebApi]
    -- Add the parameters for the stored procedure here
    @ApiUrl VARCHAR(200), --api请求地址
    @RequestType VARCHAR(5),--请求类型:POST,GET
    @ResponseText NVARCHAR(4000) OUTPUT
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    DECLARE @data varchar(8000);
    --发送数据
    set @data=''

    Declare @Object as Int
    Exec sp_OACreate 'Msxml2.ServerXMLHTTP.3.0', @Object OUT
	PRINT @Object
    Exec sp_OAMethod @Object, 'open', NULL, @RequestType,@ApiUrl,'false'
    IF @RequestType='POST'
    BEGIN
        Exec sp_OAMethod @Object, 'setRequestHeader', NULL, 'Content-Type','application/json;charset=UTF-8'
        Exec sp_OAMethod @Object, 'setRequestHeader', NULL, 'UserCode','1'
    END
    Exec sp_OAMethod @Object, 'send', NULL, @data --发送数据
    Exec sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT
    --EXEC sp_OAGetErrorInfo @Object --异常输出
    --Select  @ResponseText as ResponseText
    --Select * from [dbo].[ParseJSON](@ResponseText)
    Exec sp_OADestroy @Object

END

你可能感兴趣的:(数据库,sql,数据库)