我从ruby脚本发现了一个rails应用程序的帖子.该脚本创建一个变量请求
request = Net::HTTP::Post.new(url.path)
Run Code Online (Sandbox Code Playgroud)
然后按如下方式使用
request.content_type = "application/json"
request.body = JSON.generate( params )
response = Net::HTTP.start(url.host, url.port) {|http| http.request(request)}
Run Code Online (Sandbox Code Playgroud)
在服务器端发生了很多处理,我收到了一个Net::ReadTimeout错误
我试图指定一个超时期限
request.read_timeout = 500
Run Code Online (Sandbox Code Playgroud)
根据这个stackoverflow的答案,但我有一个
undefined method `read_timeout=' for #<Net::HTTP::Post POST> (NoMethodError)
Run Code Online (Sandbox Code Playgroud)
错误.我假设我在某处遗漏了一些简单的东西.所有线索都感激不尽
技术信息:
我想Benchmark在下面的代码中看到访问网址所花费的时间.我也试图在没有基准的情况下做同样的事情.也就是说,在测试开始和测试结束时获取时间,减去两个以获得时间.两种方法都以相同的超时错误结束.
require 'open-uri'
require 'benchmark'
response = nil
puts "opening website with benchmark..."
puts Benchmark.measure{
response = open('http://mywebsite.com')
}
puts "Done !"
status = response.status
puts status
Run Code Online (Sandbox Code Playgroud)
错误:
opening website with benchmark...
C:/ruby/lib/ruby/1.8/timeout.rb:64:in `rbuf_fill': execution expired (Timeout::Error)
from C:/ruby/lib/ruby/1.8/net/protocol.rb:134:in `rbuf_fill'
from C:/ruby/lib/ruby/1.8/net/protocol.rb:116:in `readuntil'
from C:/ruby/lib/ruby/1.8/net/protocol.rb:126:in `readline'
from C:/ruby/lib/ruby/1.8/net/http.rb:2028:in `read_status_line'
from C:/ruby/lib/ruby/1.8/net/http.rb:2017:in `read_new'
from C:/ruby/lib/ruby/1.8/net/http.rb:1051:in `request'
from C:/ruby/lib/ruby/1.8/open-uri.rb:248:in `open_http'
from C:/ruby/lib/ruby/1.8/net/http.rb:543:in `start'
from C:/ruby/lib/ruby/1.8/open-uri.rb:242:in `open_http'
from C:/ruby/lib/ruby/1.8/open-uri.rb:616:in `buffer_open'
from C:/ruby/lib/ruby/1.8/open-uri.rb:164:in `open_loop'
from C:/ruby/lib/ruby/1.8/open-uri.rb:162:in `catch'
from C:/ruby/lib/ruby/1.8/open-uri.rb:162:in `open_loop'
from C:/ruby/lib/ruby/1.8/open-uri.rb:132:in …Run Code Online (Sandbox Code Playgroud)