Ruby:使用openuri 发送http请求超时
伊谢尔伦
伊谢尔伦 2017-04-22 08:56:03
0
2
552

在官方文档看了下,好像openuri默认不支持timeout吧?
http://www.ruby-doc.org/stdlib-2.1.1/libdoc/open-uri/rdoc/OpenURI.html

如果不能设置timeout的话,有替代品吗?

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(2)
左手右手慢动作

https://github.com/lostisland/faraday

conn.get do |req|
  req.url '/search'
  req.options.timeout = 5           # open/read timeout in seconds
  req.options.open_timeout = 2      # connection open timeout in seconds
end
Peter_Zhu

No need to bother, just use Net::HTTP

uri=URI(query_uri)
Net::HTTP.get(uri,:read_timeout=>30)

This is the document introduction:

open_timeout[RW]
Number of seconds to wait for the connection to open. Any number may be used, including Floats for fractional seconds. If the HTTP object cannot open a connection in this many seconds, it raises a Net::OpenTimeout exception. The default value is nil .

By the way, please translate it:

open_timeout
Set the maximum waiting time when opening a connection. Its value can be any number, including very small floating point numbers. If the connected painting cannot be opened within the set time period, a Net::OpenTimeout exception will be thrown. The default value is nil, which means no time limit.
This will limit the request to be completed within 30 seconds.

There are also many other functions, such as setting proxy servers, CA certificates, ssl certificates, etc.
You can refer to the documentation for details, so I won’t go into details here.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!