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

9/1/2015来源:SQL技巧人气:7503

sqlserver2008 中使用MSxml2.Serverxmlhttp拼装soap调用webservice

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

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

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

实例:

declare @strXML varchar(5000)declare @obj intdeclare @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>'[email protected]+'</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,@strXMLexec sp_oagetPRoperty @obj,'status',@response outIF @hr <> 200BEGIN EXEC sp_OAGetErrorInfo @obj print @obj returnENDexec sp_oagetproperty @obj,'responseTEXT',@response out

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