上个星期,为了连接DingDing,查找了诸多资料和朋友的指点与帮助。
整理好2个存储过程,Get和Post:
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE [dbo].[usp_Util_HttpGet] @Url NVARCHAR(4000), @ResponseText NVARCHAR(MAX) OUTPUT AS BEGIN SET NOCOUNT ON DECLARE @ErrMsg VARCHAR(5000) DECLARE @token INT,@result INT,@returnTextErr VARCHAR(200),@HttpStatus VARCHAR(200) EXECUTE @result = sp_OACreate 'Msxml2.ServerXMLHTTP.3.0', @token OUT IF @result <> 0 BEGIN EXECUTE sp_OAGetErrorInfo @token, @ErrMsg OUT, @returnTextErr OUT SET @ErrMsg = '初始化对象失败,' + @ErrMsg + ISNULL(@returnTextErr, '') RAISERROR(@ErrMsg,16,-1) END EXECUTE @result = sp_OAMethod @token, 'open', NULL, 'GET', @Url,'false' IF @result <> 0 BEGIN EXECUTE sp_OAGetErrorInfo @token, @ErrMsg OUT, @returnTextErr OUT SET @ErrMsg = '创建连接失败,' + @ErrMsg + ISNULL(@returnTextErr, '') RAISERROR(@ErrMsg,16,-1) END EXECUTE @result = sp_OAMethod @token, 'setRequestHeader', NULL,'Content-Type', 'application/json; charset=UTF-8' IF @result <> 0 BEGIN EXECUTE sp_OAGetErrorInfo @token, @ErrMsg OUT, @returnTextErr OUT SET @ErrMsg = '设置RequestHeader属性失败,'+ @ErrMsg + ISNULL(@returnTextErr, '') RAISERROR(@ErrMsg,16,-1) END EXECUTE @result = sp_OAMethod @token, 'send', NULL, '' IF @result <> 0 BEGIN EXECUTE sp_OAGetErrorInfo @token, @ErrMsg OUT, @returnTextErr OUT SET @ErrMsg = '发送请求头失败,'+ @ErrMsg + ISNULL(@returnTextErr, '') RAISERROR(@ErrMsg,16,-1) END EXECUTE @result = sys.sp_OAGetProperty @token, 'Status', @HttpStatus OUT IF @result <> 0 BEGIN EXECUTE sp_OAGetErrorInfo @token, @ErrMsg OUT, @returnTextErr OUT SET @ErrMsg = '读取[Status]属性值失败,'+ @ErrMsg + ISNULL(@returnTextErr,'') RAISERROR(@ErrMsg,16,-1) END IF @HttpStatus <> 200 BEGIN SET @ErrMsg ='访问错误,http状态代码,'+ @HttpStatus +'' RAISERROR(@ErrMsg,16,1) END DECLARE @xml TABLE([ResponseText] NVARCHAR(MAX)) INSERT INTO @xml([ResponseText]) EXECUTE @result = dbo.sp_OAGetProperty @token, 'responseText' SELECT @ResponseText = [ResponseText] FROM @xml IF @result <> 0 BEGIN EXECUTE sp_OAGetErrorInfo @token, @ErrMsg OUT, @returnTextErr OUT SET @ErrMsg = '获取回复报文失败:' + ISNULL(@ErrMsg, '') + ','+ ISNULL(@returnTextErr, '') RAISERROR(@ErrMsg,16,-1) END EXECUTE @result = sp_OADestroy @token IF @result <> 0 BEGIN EXECUTE sp_OAGetErrorInfo @token, @ErrMsg OUT, @returnTextErr OUT SET @ErrMsg = '释放资源对象,' + @ErrMsg + ISNULL(@returnTextErr, '') RAISERROR(@ErrMsg,16,-1) END END
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE [dbo].[usp_Util_HttpPost] @Url NVARCHAR(4000), @PostData NVARCHAR(4000) AS BEGIN SET NOCOUNT ON DECLARE @ErrMsg VARCHAR(5000) DECLARE @token INT,@result INT,@returnTextErr VARCHAR(200),@HttpStatus VARCHAR(200) EXECUTE @result = sp_OACreate 'Msxml2.ServerXMLHTTP.3.0', @token OUT IF @result <> 0 BEGIN EXECUTE sp_OAGetErrorInfo @token, @ErrMsg OUT, @returnTextErr OUT SET @ErrMsg = '初始化对象失败,' + @ErrMsg + ISNULL(@returnTextErr, '') RAISERROR(@ErrMsg,16,-1) END EXECUTE @result = sp_OAMethod @token, 'open', NULL, 'POST', @Url,'false' IF @result <> 0 BEGIN EXECUTE sp_OAGetErrorInfo @token, @ErrMsg OUT, @returnTextErr OUT SET @ErrMsg = '创建连接失败,' + @ErrMsg + ISNULL(@returnTextErr, '') RAISERROR(@ErrMsg,16,-1) END EXECUTE @result = sp_OAMethod @token, 'setRequestHeader', NULL,'Content-Type', 'application/json; charset=UTF-8' IF @result <> 0 BEGIN EXECUTE sp_OAGetErrorInfo @token, @ErrMsg OUT, @returnTextErr OUT SET @ErrMsg = '设置RequestHeader属性失败,' + @ErrMsg + ISNULL(@returnTextErr, '') RAISERROR(@ErrMsg,16,-1) END EXECUTE @result = sp_OAMethod @token,'send', NULL, @PostData IF @result <> 0 BEGIN EXECUTE sp_OAGetErrorInfo @token, @ErrMsg OUT, @returnTextErr OUT SET @ErrMsg = '发送请求头失败,' + @ErrMsg + ISNULL(@returnTextErr, '') RAISERROR(@ErrMsg,16,-1) END EXECUTE @result = sys.sp_OAGetProperty @token, 'Status', @HttpStatus OUT IF @result <> 0 BEGIN EXECUTE sp_OAGetErrorInfo @token, @ErrMsg OUT, @returnTextErr OUT SET @ErrMsg = '读取[Status]属性值失败,' + @ErrMsg + ISNULL(@returnTextErr,'') RAISERROR(@ErrMsg,16,-1) END IF @HttpStatus <> 200 BEGIN SET @ErrMsg ='访问错误,http状态代码,'+@HttpStatus+'' RAISERROR(@ErrMsg,16,1) END IF @result <> 0 BEGIN EXECUTE sp_OAGetErrorInfo @token, @ErrMsg OUT, @returnTextErr OUT SET @ErrMsg = '获取回复报文失败:' + ISNULL(@ErrMsg, '') + ','+ ISNULL(@returnTextErr, '') RAISERROR(@ErrMsg,16,-1) END EXECUTE @result = sp_OADestroy @token IF @result <> 0 BEGIN EXECUTE sp_OAGetErrorInfo @token, @ErrMsg OUT, @returnTextErr OUT SET @ErrMsg = '释放资源对象,' + @ErrMsg + ISNULL(@returnTextErr, '') RAISERROR(@ErrMsg,16,-1) END END