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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| SET QUOTED_IDENTIFIER ON SET ANSI_NULLS ON GO
CREATE FUNCTION FN_HTTP_POST ( @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;
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
|