我在使用 Ruby 2.0p0 和 XMLRPC::Client 时遇到问题。当我在 2 个不同版本的 ruby 中运行下面的代码时,我在 1.9.3 上得到了正确的响应,但在 2.0.0 上得到了错误。有同样问题的人吗?解决方案是不使用最新版本的 ruby 还是有解决方法?
require "xmlrpc/client"
server = XMLRPC::Client.new2('http://api.flickr.com/services/xmlrpc/')
begin
res = server.call('flickr.test.echo')
puts res
rescue XMLRPC::FaultException => e
puts e.faultCode
puts e.faultString
end
使用 ruby-1.9.3-p392 [x86_64]
我从 flickr 得到了正确的响应,因为我没有提供 API key :
100
Invalid API Key (Key has invalid format)
使用 ruby-2.0.0-p0 [x86_64]
我从 ruby 那里得到一个错误,说“错误的大小。是 365,应该是 207 (RuntimeError)”
/home/luisramalho/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/xmlrpc/client.rb:506:in `do_rpc': Wrong size. Was 365, should be 207 (RuntimeError)
from /home/luisramalho/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/xmlrpc/client.rb:281:in `call2'
from /home/luisramalho/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/xmlrpc/client.rb:262:in `call'
from xmlrpc.rb:5:in `<main>'
最佳答案
我在使用 ruby2 访问不同的 xml rpc api(upcdatabase.com 的)(说真的,谁还在使用 xml rpc api?)时遇到了类似的问题。
我的解决方案是使用与 ruby 的默认库不同的 xmlrpc 库。 LibXML-XMLRPC。它使用 c 扩展,应该比标准库更快,但它最后一次更新是在 2008 年,所以谁知道这个说法在今天有多真实。
这就是我的代码最终起作用的原因。
require 'xml/libxml/xmlrpc'
require 'net/http'
net = Net::HTTP.new("www.upcdatabase.com", 80)
server = XML::XMLRPC::Client.new(net, "/xmlrpc")
result = server.call('lookup', 'rpc_key' => "YOLOSWAG", 'upc' => "071160055506")
希望这对您有所帮助。
关于Ruby 2.0p0 和 XMLRPC::Client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15640764/