SQL呼叫WebService介面

2021-09-22 22:12:54 字數 4026 閱讀 4489

今天在做乙個很奇葩的東西,中間有個過程要在sql觸發器裡面呼叫webservice介面。呵呵~

alter trigger tgr_updatememcached

on dbo.[user]

after update as

--獲得更新前的資料

--select * from deleted as olddata

--獲得更新後的資料

--select * from inserted as newdata

--呼叫webservice----------------

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 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

goset quoted_identifier on

goalter function [dbo].[urlencode]

( @param nvarchar(2000)

)returns varchar(max)

asbegin

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)

endelse

set @output = @output + '%u' + right(@s, 2) + left(@s, 2)

--move to next hex

set @i = @i + 1

endreturn @output

end

同時要注意webservice裡面的方法喲:

[webmethod]

public bool updatecache(string usercode, string username, string userage)

);}

動態呼叫WebService

public static object invokewebservice string url,string methodname,object args 其中,url是web服務的位址,methodname是要呼叫服務方法名,args是要呼叫web服務所需的引數,返回值就是web服務返回的結果了...

呼叫WebService出錯

呼叫乙個遠端webservice編譯出錯 錯誤2 型別 system.web.services.protocols.soaphttpclientprotocol 在未被引用的程式集中定義。必須新增對程式集 system.web.services,version 2.0.0.0,culture neu...

webservice建立呼叫

在乙個分布式應用上面用到了webservice,記錄一下。1 建立web專案 xfire 2 編寫介面以及實現類 3 在web info下面新建meta inf資料夾,新建services.xml。內容如下 名稱介面全稱 介面實現 4 編輯web.xml對service攔截 6 客戶端呼叫,這裡面就...