SQL Server 访问URL 调用WebServer

SQL Server 访问URL 调用WebServer

以下整理的SQL Server中访问URL地址的方法,并已封装成存储过程,可以实现POST/GET请求

SET QUOTED_IDENTIFIER ON
SET ANSI_NULLS ON
GO
/**
存储过程发起URL请求

启用 Ole Automation Procedures 选项
exec sp_configure 'show advanced options',1;
go
reconfigure;
go
sp_configure 'Ole Automation Procedures',1;
go
reconfigure;
go
*/
ALTER PROC P_Url_SendRequest
    (
      @Url VARCHAR(8000) = '' ,
      @PostData VARCHAR(8000) = '' ,
      @ResponseText VARCHAR(8000) = '' OUTPUT
    )
AS
    SET NOCOUNT ON 


    DECLARE @ServiceUrl AS VARCHAR(1000) 
    DECLARE @UrlAddress VARCHAR(500) ,
        @ErrMsg VARCHAR(5000)


    SET @ServiceUrl = @Url 

    PRINT @ServiceUrl

    DECLARE @Object AS INT ,
        @status INT ,
        @returnText AS VARCHAR(8000) ,
        @HttpStatus VARCHAR(200) ,
        @HttpMethod VARCHAR(20) = 'get'

    IF ISNULL(@PostData, '') <> ''
        SET @HttpMethod = 'post'

/*初始化对*/                   
    EXEC @status = sp_OACreate 'Msxml2.ServerXMLHTTP.3.0', @Object OUT;
    IF @status <> 0
        BEGIN  
            EXEC sp_OAGetErrorInfo @Object, @ErrMsg OUT, @returnText OUT
            SET @ErrMsg = '初始化对象失败,' + @ErrMsg + ISNULL(@returnText, '')
            RAISERROR(@ErrMsg,16,-1)           
            RETURN 1
        END  
/*创建链接*/  
    EXEC @status = sp_OAMethod @Object, 'open', NULL, @HttpMethod, @ServiceUrl,
        'false'
    IF @status <> 0
        BEGIN  
            EXEC sp_OAGetErrorInfo @Object, @ErrMsg OUT, @returnText OUT
            SET @ErrMsg = '创建连接失败,' + @ErrMsg + ISNULL(@returnText, '')
            RAISERROR(@ErrMsg,16,-1)           
            RETURN 2
        END  

    SELECT  @HttpMethod

    IF @HttpMethod = 'post'
        BEGIN
            --EXEC @status = sp_OAMethod @Object, 'setRequestHeader',
            --    'Content-Type', 'application/x-www-form-urlencoded'   

            EXEC @status = sys.sp_OAMethod @Object, 'setRequestHeader', NULL,
                'Content-Type', 'application/x-www-form-urlencoded';
        END
    ELSE
        BEGIN

            EXEC @status = sp_OAMethod @Object, 'setRequestHeader', NULL,
                'Content-Type', 'text/xml; charset=gb2312'
            PRINT @status
        END


    IF @status <> 0
        BEGIN  
            EXEC sp_OAGetErrorInfo @Object, @ErrMsg OUT, @returnText OUT

            SET @ErrMsg = '设置RequestHeader属性失败,' + @ErrMsg
                + ISNULL(@returnText, '')
            RAISERROR(@ErrMsg,16,-1)           
            RETURN 2
        END  

    EXEC @status = sp_OAMethod @Object, 'send', NULL, @PostData
    IF @status <> 0
        BEGIN  
            EXEC sp_OAGetErrorInfo @Object, @ErrMsg OUT, @returnText OUT 
            SET @ErrMsg = '发送请求头失败,' + @ErrMsg + ISNULL(@returnText, '')
            RAISERROR(@ErrMsg,16,-1)           
            RETURN 3
        END 


    EXEC @status = sys.sp_OAGetProperty @Object, 'Status', @HttpStatus OUT;

    IF @status <> 0
        BEGIN
            EXEC sp_OAGetErrorInfo @Object, @ErrMsg OUT, @returnText OUT 
            SET @ErrMsg = '读取[Status]属性值失败,' + @ErrMsg + ISNULL(@returnText,
                                                              '')
            RAISERROR(@ErrMsg,16,-1)           
            RETURN 3

        END

    IF @HttpStatus <> 200
        BEGIN
            SET @ErrMsg = '访问错误,http状态代码,' + @HttpStatus
            RAISERROR(@ErrMsg,16,1);
            RETURN -6;
        END


    EXEC @status = sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT
    IF @status <> 0
        BEGIN  
            EXEC sp_OAGetErrorInfo @Object, @ErrMsg OUT, @returnText OUT
            SET @ErrMsg = '获取回复报文失败,' + ISNULL(@ErrMsg, '')
                + ISNULL(@returnText, '') + ISNULL(@returnText, '')
            RAISERROR(@ErrMsg,16,-1)           
            RETURN 4
        END 

    EXEC @status = sp_OADestroy @Object
    IF @status <> 0
        BEGIN  
            EXEC sp_OAGetErrorInfo @Object, @ErrMsg OUT, @returnText OUT
            SET @ErrMsg = '释放资源对象,' + @ErrMsg + ISNULL(@returnText, '')
            RAISERROR(@ErrMsg,16,-1)           
            RETURN 5
        END


    RETURN 0

GO

调用WebServer的实例请参考,另外一位兄弟的博客http://www.cnblogs.com/icycore/p/3532197.html

评论 2 您还未登录,请先 登录 后发表或查看评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

madStone_l

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值