1、开启通信配置
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
2、创建请求函数
CREATE FUNCTION dbo.HTTP_POST_FUNC(
@URL VARCHAR(256),
@DATA VARCHAR(2000),
@REQ_H_ACCEPT VARCHAR(256),
@REQ_H_CONTENT_TYPE VARCHAR(256)
)
RETURNS VARCHAR(5000)
AS
BEGIN
DECLARE
@object int,
@returnStatus int,
@returnText varchar(5000),
@errMsg varchar(2000),
@httpStatus varchar(20);
/* 初始化 */
EXEC @returnStatus = SP_OACreate 'Msxml2.ServerXMLHTTP.3.0',@object OUT;
IF @returnStatus <> 0
BEGIN
EXEC SP_OAGetErrorInfo @object, @errMsg OUT, @returnText OUT;
RETURN ('初始化对象失败,' + @errMsg + ISNULL(@returnText,''));
END
/* 创建链接 */
EXEC @returnStatus= SP_OAMethod @object,'open',NULL,'post',@URL,'false';
IF @returnStatus <> 0
BEGIN
EXEC SP_OAGetErrorInfo @object, @errMsg OUT, @returnText OUT;
RETURN ('创建连接失败,' + @errMsg + ISNULL(@returnText, ''));
END
EXEC @returnStatus=SP_OAMethod @object,'setRequestHeader',NULL,'Accept',@REQ_H_ACCEPT;
EXEC @returnStatus=SP_OAMethod @object,'setRequestHeader',NULL,'Content-Type',@REQ_H_CONTENT_TYPE;
EXEC @returnStatus=SP_OAMethod @object,'setRequestHeader',NULL,'Content-Length','1000000';
/* 发起请求 */
EXEC @returnStatus= SP_OAMethod @object,'send',NULL,@DATA;
IF @returnStatus <> 0
BEGIN
EXEC SP_OAGetErrorInfo @object, @errMsg OUT, @returnText OUT;
RETURN ('发起请求失败,' + @errMsg + ISNULL(@returnText, ''));
END
/* 获取HTTP状态代码 */
EXEC @returnStatus = SP_OAGetProperty @Object, 'Status', @httpStatus OUT;
IF @returnStatus <> 0
BEGIN
EXEC sp_OAGetErrorInfo @Object, @errMsg OUT, @returnText OUT;
RETURN ('获取HTTP状态代码失败,' + @errMsg + ISNULL(@returnText, ''));
END
IF @httpStatus <> 200
BEGIN
RETURN ('访问错误,HTTP状态代码:' + @httpStatus);
END
/* 获取返回信息 */
EXEC @returnStatus= SP_OAGetProperty @object,'responseText',@returnText OUT;
IF @returnStatus <> 0
BEGIN
EXEC SP_OAGetErrorInfo @object, @errMsg OUT, @returnText OUT;
RETURN ('获取返回信息失败,' + @errMsg + ISNULL(@returnText, ''));
END
RETURN @returnText;
END;
3、调用请求函数
select dbo.HTTP_POST_FUNC('http://127.0.0.1:8081/dataApi',
'{"operation":"postHttpRequest"}',
'application/json',
'application/json');
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于