【问题标题】:EOFError (end of file reached) in Ruby on Rails with http.requestRuby on Rails 中带有 http.request 的 EOFError(到达文件末尾)
【发布时间】:2014-04-15 09:49:05
【问题描述】:

我正在尝试获取 json 表单网址:

uri = URI.parse("http://84.38.185.251:9262/send")

http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)

response = http.request(request)

response.code             # => 301
response.body             # => The body (HTML, XML, blob, whatever)
response["cache-control"] # => public, max-age=2592000
puts response.body

但我得到一个错误:`EOFError(到达文件末尾): app/controllers/sensors_controller.rb:35:in sensinfo'

sensors_controller.rb:35:

response = http.request(request)

我做错了什么?

【问题讨论】:

    标签: ruby-on-rails ruby http get


    【解决方案1】:

    这个错误主要是因为使用 https

    如果是https那么

    请试试这个

    uri = URI.parse("https://84.38.185.251:9262/send")
    http = Net::HTTP.new(uri.host, uri.port)
    request = Net::HTTP::Get.new(uri.request_uri)
    http.use_ssl = true  
    response = http.request(request)
    

    注意补充

    http.use_ssl = true
    

    如果不是https

    http.use_ssl = false
    

    或者你可以添加条件

    http.use_ssl = true if domain =~ /^https/
    

    你可以在这个http://expressica.com/2012/02/10/eoferror-end-of-file-reached-issue-when-post-a-form-with-nethttp/获得更多信息

    【讨论】:

    • 我已将 http.use_ssl 设置为 true 和一个明确的“https://”(请自行查看——我的代码位于 github.com/betesh/social_count/blob/v0.0.7/lib/social_count/…)但仍然看到此错误一次,堆栈跟踪结束在 openssl/buffering.rb:174:in sysread_nonblock'` 从 http.request(Net::HTTP::Get.new(uri)) 调用。我怀疑有时连接会由于硬件/电源故障而突然终止,因此永远无法 100% 保证不会发生这种情况。但这只是猜测。
    【解决方案2】:

    我认为这是某种错误; typhoeus 似乎有效:

    require 'typhoeus'
    
    response = Typhoeus.get("http://84.38.185.251:9262/send")
    p response.body
    #=> {"ids":"-1","data":{"temp":"nan","h":"-1"},"status":"255","voltage":"-1"}
    

    【讨论】:

    • 需要其他宝石吗?它给了我一个错误 `require': No such file to load -- typhoeus (LoadError) 但我将它包含在 gem 列表中并进行了捆绑安装
    • 不,它不会,它应该与gem 'typhoeus'Gemfile 中正常工作
    • 我只是重启了服务器)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-15
    • 1970-01-01
    • 2013-05-25
    • 2011-07-11
    • 2017-02-28
    • 1970-01-01
    • 2017-12-29
    相关资源
    最近更新 更多