1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| SET QUOTED_IDENTIFIER ON SET ANSI_NULLS ON GO CREATE FUNCTION FN_HTTP_GET( @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,'get',@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
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 ; GO
|