Ruby Net::HTTP 使用 https 时获取请求错误

标签 ruby https get net-http

我有一个网络服务器,我需要从中获取一些数据。我想重用连接,这就是我选择 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/

相关文章:

python - HTTP请求相当于 `curl --user`参数?

ruby-on-rails - Rails 作业生成器不工作

mysql - 更新 JSON 列花费的时间太长

security - 去除 HTTPS 页面中的 IE 警告

json - 使用jquery从API获取数据

php - url 上的 Opencart token 是 session ID 吗?

与 YAML 之间的 Ruby 自定义类;

ruby - 无法将 Ruby block cat 到文件

https - Charles 未能获取 https 请求

nginx - nginx将HTTPS重定向到HTTP