sqlserver2008 中使用MSXML2.ServerXMLHttp拼装soap调用webservice

时间:2022-08-07 19:37:24

要调用的接口方法:UP_ACC_inst_Info(string xml)

接口参数:xml格式的字符串

接口功能:传递人员编号、备注到接口进行更新,接口返回更新结果。

实例:

declare @strXML varchar(5000)
declare @obj int
declare @sUrl varchar(5000)
declare @response varchar(5000)
declare @hr int
--参数
SET @strXML ='<root><accountid>654</accountid> <innotes>654的备注</innotes></root>'

--将参数中的<,>转换为转义字符,否则接口无法把@strXML识别为string类型,会报400错误信息
set @strxml=replace(@strXML,'<','&lt;')
set @strxml=replace(@strXML,'>','&gt;')

--接口地址
set @sUrl='http://localhost/WebService.asmx'
--拼装soap 1.2 格式信息
set @strXML='<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"><soap12:Body><UP_ACC_inst_Info xmlns="http://tempuri.org/"><strXML>'+@strXML+'</strXML></UP_ACC_inst_Info></soap12:Body></soap12:Envelope>'

exec sp_OACreate 'MSXML2.ServerXMLHttp', @obj out

exec sp_OAMethod @obj,'Open',null,'POST',@sUrl,FALSE

exec sp_OAMethod @obj,'setRequestHeader',null,'Content-Type','application/soap+xml; charset=utf-8'
exec sp_OAMethod @obj,'Send',null,@strXML
exec sp_oagetproperty @obj,'status',@response out
IF @hr <> 200
BEGIN
EXEC sp_OAGetErrorInfo @obj
print @obj
return
END
exec sp_oagetproperty @obj,'responseTEXT',@response out

--由于接口返回的结果也是xml格式的string,将里边的<,>转换回来
set @response=replace(@response,'&lt;','<')
set @response=replace(@response,'&gt;','>')
print @response
exec sp_oadestroy @obj