sp_configure 'show advanced options' , 1 ;
GO
RECONFIGURE ;
GO
sp_configure 'Ole Automation Procedures' , 1 ;
GO
RECONFIGURE ;
GO
EXEC sp_configure 'Ole Automation Procedures' ;
GO
SET QUOTED_IDENTIFIER ON
SET ANSI_NULLS ON
GO
create 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'
EXEC @status = sys. sp_OAMethod @Object , 'setRequestHeader' , NULL , 'Content-Type' , 'application/x-www-form-urlencoded' ;
ELSE
EXEC @status = sp_OAMethod @Object , 'setRequestHeader' , NULL , 'Content-Type' , 'text/xml; charset=gb2312'
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