Ruby http/net 中连接超时问题
2017-11-15 19:21
555 查看
下面在调用币安的接口时,经常会卡住,设置连接超时也不会抛出异常,代码如下(默认连接超时为nil, 参考:https://github.com/ruby/ruby/pull/269):
require 'net/http' require 'json' require 'byebug' uri = URI('https://www.binance.com/api/v1/ticker/24hr?symbol=EOSBTC') req = Net::HTTP::Get.new(uri) loop do res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http| http.open_timeout = 1 http.request(req) end puts "#{JSON.parse res.body}" puts "http request complete." sleep 1 end
不知道为什么,用
http.open_timeout = 1这种设置不奏效,改为在start的options中配置即可则会超时抛出异常,如下:
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https', :open_timeout => 1) do |http| http.request(req) end
相关文章推荐
- 在多线程环境下使用HttpWebRequest或者调用Web Service(连接报超时问题)
- Android开发-----关于解决Retrofit打印HttpLog和设置连接超时的问题
- 解决Win7中HttpWebRequest连接缓慢、超时等问题
- 【问题】spark运行python写的mapreduce任务,hadoop平台报错,java.net.ConnectException: 连接超时
- 解决登录提示http连接超时问题
- System.Net.HttpWebRequest.GetRequestStream超时问题
- 解决Win7中第一次HttpWebRequest连接缓慢、超时等问题
- 【转】System.Net.HttpWebRequest请求超时的问题解决方法
- android http连接阻塞超时问题
- plsql、dblink连接Oracle数据库长时间空闲后自动断开及远程终端连接linux空闲超时后自动断开问题
- httpClient java.net.URLEncoder.encode二次转码问题
- 转:使用plSQL连接Oracle的问题(SQL*Net not properly installed)
- Asp.Net服务器发送HTTP标头后无法设置内容类型的问题解决
- zookeeper连接超时问题排查
- https,https的本地测试环境搭建,asp.net结合https的代码实现,http网站转换成https网站之后遇到的问题
- ASP.NET在使用EF时遇到的“无效操作 连接被关闭”问题的摸索
- VS2008在做简单的ado.net连接数据库时出现的问题
- ASP.NET连接 MS SQL Timeout expired. 问题的处理
- ASP.NET与Mysql数据库连接问题