• MS SQL SERVER操作API的GET和POST存储过程



    上个星期,为了连接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
    Source Code
    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
    Source Code



  • 相关阅读:
    Java标签实现分页
    Servlet过滤器介绍之原理分析
    java超强分页标签演示
    Mybatis中resultMap与resultType区别
    洛谷 P1002 过河卒
    洛谷 P2181 对角线
    ACM常数优化
    2020 年百度之星&#183;程序设计大赛
    AtCoder Beginner Contest 171 E
    AtCoder Beginner Contest 171 D
  • 原文地址:https://www.cnblogs.com/insus/p/15205077.html
Copyright © 2020-2023  润新知