SQL调用WebService接口
今天在做一个非常奇葩的东西。中间有个过程要在SQL触发器里面调用webservice接口。呵呵~
ALTER TRIGGER tgr_UpdateMemcached ON dbo.[User] AFTER UPDATE AS --获得更新前的数据 --SELECT * FROM Deleted AS OldData --获得更新后的数据 --SELECT * FROM Inserted AS NewData --调用webService---------------- declare @ServiceUrl as nvarchar(1000) DECLARE @UrlAddress nvarchar(500) --WebService地址:以http开头,结尾带斜杠。比如'http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/' set @UrlAddress = 'http://localhost/webService_UpdateCache/WebService1.asmx' DECLARE @FunName nvarchar(50)--WebService中调用的方法名:比如'getMobileCodeInfo' SET @FunName = 'UpdateCache' --以下參数相应WebService中4个參数的[參数名] declare @UserCode nvarchar(36),@UserName nvarchar(36),@UserAge nvarchar(36) SET @UserCode='UserCode' SET @UserName='UserName' SET @UserAge='UserAge' --拼接參数名称 DECLARE @UserCode_Value nvarchar(50)='0000',@UserName_Value nvarchar(50)='0000',@UserAge_Value nvarchar(50)='0000' SELECT * FROM Inserted SELECT @UserCode_Value=LTrim(RTrim(UserCode)),@UserName_Value=LTrim(RTrim(UserName)),@UserAge_Value=LTrim(RTrim(UserAge)) FROM Inserted ;--从inserted表给參数赋值 --拼接地址 SET @ServiceUrl = @UrlAddress +'/'+ @FunName + '?' + @UserCode + '=' + [dbo].[UrlEncode](@UserCode_Value) +'&' + @UserName + '=' +[dbo].[UrlEncode](@UserName_Value) +'&' + @UserAge + '=' + [dbo].[UrlEncode](@UserAge_Value) --SET @ServiceUrl = @UrlAddress +'/'+ @FunName + '?' + @UserCode + '=' + @UserCode_Value +'&' + @UserName + '=' +@UserName_Value +'&' + @UserAge + '=' + @UserAge_Value --SELECT @ServiceUrl --查看拼接地址,调试时打开 --訪问地址获取结果 Declare @Object as Int Declare @ResponseText as nvarchar(4000) EXEC sp_OACreate 'MSXML2.XMLHTTP', @Object OUT; --创建OLE组件对象 Exec sp_OAMethod @Object, 'open', NULL, 'post',@ServiceUrl,'false' --打开链接,注意是get还是post EXEC sys.sp_OAMethod @object,'setRequestHeader',NULL,'Content-Type','application/x-www-form-urlencoded;charset=UTF-8' Exec sp_OAMethod @Object, 'send' EXEC sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT --输出參数 Select @ResponseText --输出结果 Exec sp_OADestroy @Object GO
为了使SQL能够想代码里面对參数进行编码和解码,还须要增加以下自己定义函数:
USE [TestMemcached] GO /****** Object: UserDefinedFunction [dbo].[UrlEncode] Script Date: 2015/7/6 21:36:27 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER FUNCTION [dbo].[UrlEncode] ( @Param NVARCHAR(2000) ) RETURNS VARCHAR(MAX) AS BEGIN DECLARE @HexStr VARCHAR(MAX) --Use system function to convert input string to hex string SET @HexStr = master.dbo.fn_varbintohexstr(CONVERT(VARBINARY(MAX), @Param)) --Remove the starting '0x' SET @HexStr = RIGHT(@HexStr, LEN(@HexStr)-2) --Declare required variables DECLARE @I INT, @Len INT DECLARE @Output VARCHAR(MAX), @S CHAR(4), @C CHAR(1) DECLARE @LoByte TINYINT, @HiByte TINYINT --Get length SET @Len=LEN(@HexStr)/4 --Start with first character SET @I=0 --Prepare the output string SET @Output='' WHILE @I<@Len BEGIN SET @S=SUBSTRING(@HexStr, @I*4 + 1, 4) IF RIGHT(@S, 2)='00' BEGIN --Try to convert 2 hex digits to char SET @LoByte = ASCII(SUBSTRING(@S, 2, 1)) - 48 IF @LoByte>10 SET @LoByte = @LoByte - 39 --0x61'a'-> 10 SET @HiByte = ASCII(SUBSTRING(@S, 1, 1)) - 48 IF @HiByte>10 SET @HiByte = @HiByte - 39 SET @C=CHAR(@LoByte + @HiByte * 16) --If it's a reserved character, don't encode IF @C LIKE '[A-Za-z0-9()''*-._! ]' SET @Output = @Output + @C ELSE SET @Output = @Output + '%' + LEFT(@S, 2) END ELSE SET @Output = @Output + '%u' + RIGHT(@S, 2) + LEFT(@S, 2) --Move to next hex SET @I = @I + 1 END RETURN @Output END
同一时候要注意webservice里面的方法哟:
[WebMethod] public bool UpdateCache(string UserCode, string UserName, string UserAge) { // HttpContext.Current.Response.ContentType = "application/x-www-form-urlencoded;charset=UTF-8"; //string action = HttpUtility.UrlDecode(HttpContext.Current.Request.QueryString["UserName"]); // string a = HttpContext.Current.Request.QueryString["UserName"]; UserCode = HttpUtility.UrlDecode(UserCode); UserName = HttpUtility.UrlDecode(UserName); UserAge = HttpUtility.UrlDecode(UserAge); return MemCache.GetInstance().Replace<ClassLib.User>(UserCode, new ClassLib.User() { UserCode = UserCode, UserName = UserName, UserAge = UserAge }); }
posted on 2017-04-17 18:43 cynchanpin 阅读(5581) 评论(0) 编辑 收藏 举报