我有一个网络服务器,我需要从中获取一些数据。我想重用连接,这就是我选择 HTTP.new
require "net/http"
require "openssl"
uri = URI.parse("https://example.com")
path = "/index.html"
http = Net::HTTP.new(uri.host, uri.port,
:use_ssl => uri.scheme == 'https')
request = Net::HTTP::Get.new "/index.html"
response = http.request(request)
当发出请求 (response = http.request(request)
) 时,ruby 会抛出一个 TypeError
:
TypeError: no implicit conversion of Hash into String
from /usr/local/Cellar/ruby/2.2.2/lib/ruby/2.2.0/net/http.rb:879:in `initialize'
from /usr/local/Cellar/ruby/2.2.2/lib/ruby/2.2.0/net/http.rb:879:in `open'
from /usr/local/Cellar/ruby/2.2.2/lib/ruby/2.2.0/net/http.rb:879:in `block in connect'
from /usr/local/Cellar/ruby/2.2.2/lib/ruby/2.2.0/timeout.rb:74:in `timeout'
from /usr/local/Cellar/ruby/2.2.2/lib/ruby/2.2.0/net/http.rb:878:in `connect'
from /usr/local/Cellar/ruby/2.2.2/lib/ruby/2.2.0/net/http.rb:863:in `do_start'
from /usr/local/Cellar/ruby/2.2.2/lib/ruby/2.2.0/net/http.rb:852:in `start'
from /usr/local/Cellar/ruby/2.2.2/lib/ruby/2.2.0/net/http.rb:1375:in `request'
from (irb):14
from /usr/local/bin/irb:11:in `<main>'
当我使用 http 时同样的请求有效
最佳答案
我不确定您是否使调用过于复杂。为什么不直接使用:
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
我的假设是您已经知道您的目标服务器是否需要 HTTPS。
关于Ruby Net::HTTP 使用 https 时获取请求错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32509800/