Ruby on Rails Net :: HTTP use_ssl抛出'undefined method'

Chr*_*ris 14 ruby-on-rails

这是我的代码:

sock = Net::HTTP.new(url.host, url.port)
sock.use_ssl = true
response = sock.start {|http| http.request(req)}
Run Code Online (Sandbox Code Playgroud)

这是错误:

undefined method `use_ssl=' for #<Net::HTTP www.paypal.com:443 open=false>
Run Code Online (Sandbox Code Playgroud)

谷歌让我什么都没有!

谢谢.

DGM*_*DGM 18

除了'net/http'之外,还需要'net/https'.然后use_ssl=将定义.

require 'net/http'
require 'net/https'
connection = Net::HTTP::new 'www.example.com'
connection.use_ssl = true
Run Code Online (Sandbox Code Playgroud)


Mit*_*sey 5

那是因为函数是sock.use_ssl?,它返回一个布尔值,它不是一个setter方法.

它似乎总是返回false.它在Net::HTTPS包中被覆盖,如果你想做任何SSL的东西,这可能是你应该使用的.

这是ruby doc