公司以前使用的 1.8.6 的 ruby,现在要切换到 2.0, 需要对 soap 调用进行改写,2.0 没有 soap 了,只好用 savon
使用的 ruby2.0 代码如下
savon 的版本为 2.5.1
client = Savon.client do
wsdl "http://10.112.222.174:13001/gateway/XXXX?wsdl"
end
xml = "<request id='#{Time.now.to_i}' >
<cmd function='getTestbedList'>
<para name='username'>
<value>admin</value>
</para>
<para name='password'>
<value>admin</value>
</para>
</cmd>
</request>"
response = client.call(:get_testbed_list,xml: xml)
p response
报错如下
C:/Ruby200/lib/ruby/gems/2.0.0/gems/savon-2.5.1/lib/savon/response.rb:85:in `raise_soap_and_http_errors!': (soapenv:Server.userException) org.xml.sax.SAXException: Bad envelope tag: request (Savon::SOAPFault)
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/savon-2.5.1/lib/savon/response.rb:14:in `initialize'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/savon-2.5.1/lib/savon/operation.rb:64:in `new'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/savon-2.5.1/lib/savon/operation.rb:64:in `create_response'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/savon-2.5.1/lib/savon/operation.rb:55:in `call'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/savon-2.5.1/lib/savon/client.rb:36:in `call'
使用 ruby1.8.6 可以成功调用
require 'rubygems'
require 'wsdl/parser'
require 'wsdl/soap/wsdl2ruby'
require 'soap/rpc/standaloneServer'
require 'soap/wsdlDriver'
require 'jcode'
require 'socket'
require 'iconv'
@wsdl = ::SOAP::RPC::Driver.new("http://10.112.222.174:13001/gateway/XXXX")
@wsdl.add_method("getTestbedList", "requestXml")
xml = "<request id='#{Time.now.to_i}' >
<cmd function='getTestbedList'>
<para name='username'>
<value>admin</value>
</para>
<para name='password'>
<value>admin</value>
</para>
</cmd>
</request>"
xml = Iconv.iconv("UTF-8", "", xml).to_s
$KCODE = "UTF8"
response_str = @wsdl.call("getTestbedList", xml)
puts response_str
$KCODE = 'E'