Net::HTTP get timeout in Ruby

為{幸葍}努か 提交于 2019-12-01 21:25:26

问题


How can I set a larger timeout in net/http? What I'm doing is this:

rta = JSON.parse(Net::HTTP.get(URI(url)))

I've tried:

uri = URI(url)
http = Net::HTTP.new(uri.host, uri.port)
http.open_timeout = 5* 60
http.read_timeout = 5* 60
rta = JSON.parse(Net::HTTP.get(URI(url)))

but it still doesn't work.


回答1:


It looks like it probably isn't working because you're making your get call on the Class instead of the instance you created. Try changing that last line to:

rta = JSON.parse(http.get(URI(url)))



回答2:


Maybe you can use OpenURI :

require 'open-uri'
open(url, :read_timeout => 5 * 60) do |file|
  rta = JSON.parse file
  # ...
end


来源:https://stackoverflow.com/questions/19325479/nethttp-get-timeout-in-ruby

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!