Ruby HTTP POST-错误 [英] Ruby HTTP POST - Errors

查看:79
本文介绍了Ruby HTTP POST-错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释为什么在执行此POST时出现此错误吗?我从 Ruby-docs中提取了该代码段页面.

Can someone explain to me why I am getting this error when doing this POST? I pulled the snippet from the Ruby-docs page.

undefined method `hostname' for #URI::HTTP:0x10bd441d8 URL:http://ws.mittthetwitapp.com/ws.phpmywebservice (NoMethodError)

也许我错过了需求之类的东西吗?

Perhaps I am missing a require or something?

require 'net/http'

uri= URI('http://ws.mywebservice.com/ws.php')
req = Net::HTTP::Post.new(uri.path)
req.set_form_data('xmlPayload' => '<TestRequest><Message>Hi Test</Message></TestRequest>')

res = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end

case res
 when Net::HTTPSuccess, Net::HTTPRedirection
 # OK
else
 res.value
end

推荐答案

如果您使用的是1.9.3之前的Ruby版本,则应使用uri.host.

If you're using a version of Ruby prior to 1.9.3, you should use uri.host.

URI#hostname是在Ruby 1.9.3中添加的. URI#host不同之处在于,它从IPv6主机名中删除了括号.对于非IPv6主机名,其行为应相同.

URI#hostname was added in Ruby 1.9.3. It is different than URI#host in that it removes brackets from IPv6 hostnames. For non-IPv6 hostnames it should behave identically.

实现(来自 APIdock ):

def hostname
  v = self.host
  /\A\[(.*)\]\z/ =~ v ? $1 : v
end

这篇关于Ruby HTTP POST-错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆