require "net/http" @r = Net::http.get_response(URI.parse(myURL)) return @r.code
然而,对于一些URL(大多数是指向诸如Web计数器的奇怪的东西,不会给出适当的响应)我得到一个未定义的方法request_uri for#exception.我已经追溯到http.rb的第380行(我正在运行Ruby 1.8),它说:
def http.get_response(uri_or_host,path = nil,port = nil,&block) if path host = uri_or_host new(host,port || http.default_port).start {|http| return http.request_get(path,&block) } else uri = uri_or_host new(uri.host,uri.port).start {|http| return http.request_get(uri.request_uri,&block) <--- liNE 380 } endend
导致这个异常的原因我很失落.我会期望一个URI :: InvalIDURIError,但不是这样.
解决方法 给定一个认可的方案(例如http / https),一个更专业的类将被实例化.否则创建“通用”URI; “请求URI”的想法只对某些URI方案有意义.例:
irb(main):001:0> require 'uri'=> trueirb(main):002:0> URI.parse('http://www.Google.com/').class=> URI::httpirb(main):003:0> URI.parse('https://www.Google.com/').class=> URI::httpSirb(main):004:0> URI.parse('foo://www.Google.com/').class=> URI::Genericirb(main):005:0> URI.parse('http://www.Google.com/').respond_to?(:request_uri)=> trueirb(main):006:0> URI.parse('https://www.Google.com/').respond_to?(:request_uri)=> trueirb(main):007:0> URI.parse('foo://www.Google.com/').respond_to?(:request_uri)=> false
所以您解析的URI之一有一个奇怪的方案 – 即使它是一个有效的URI – 这就是全部.
总结以上是内存溢出为你收集整理的红宝石 – 加载网页时:“undefined方法`request_uri’for#”全部内容,希望文章能够帮你解决红宝石 – 加载网页时:“undefined方法`request_uri’for#”所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)