SQL SERVER HTTP请求

时间:2024-10-30 12:04:20
--开启Sql Server 通讯配置--
sp_configure 'show advanced options', 1;
GO
RECONFIGURE WITH OVERRIDE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE WITH OVERRIDE;
GO
EXEC sp_configure 'Ole Automation Procedures';
GO
/*
参数说明?
@URL=http请求地址
@Status=状态代码
@ReturnText=返回码
@Object=对象令牌
@ErrSrc=错误源编码
*/
CREATE PROCEDURE [dbo].[HttpRequest](
@URL NVARCHAR(4000),
@Status INT=0 OUTPUT,
@ReturnText NVARCHAR(4000)='' OUT
)
AS
BEGIN
DECLARE
@Object int,
@ErrSrc int /*初始化对象*/
EXEC @Status = SP_OACreate 'Msxml2.ServerXMLHTTP.3.0', @Object OUT
IF @Status <> 0
BEGIN
EXEC SP_OAGetErrorInfo @Object, @ErrSrc OUT, @ReturnText OUT
RETURN
END /*创建链接*/
EXEC @Status= SP_OAMethod @Object,'open',NULL,'GET',@URL
IF @Status <> 0
BEGIN
EXEC SP_OAGetErrorInfo @Object, @ErrSrc OUT, @ReturnText OUT
RETURN
END
EXEC @Status=SP_OAMethod @Object,'setRequestHeader','Content-Type','application/x-www-form-urlencoded' /*发起请求*/
EXEC @Status= SP_OAMethod @Object,'send',NULL
IF @Status <> 0
BEGIN
EXEC SP_OAGetErrorInfo @Object, @ErrSrc OUT, @ReturnText OUT
RETURN
END /*获取返回*/
EXEC @Status= SP_OAGetProperty @Object,'responseText',@ReturnText OUT
IF @Status <> 0
BEGIN
EXEC SP_OAGetErrorInfo @Object, @ErrSrc OUT, @ReturnText OUT
RETURN
END
END;
GO