以下是要运行的代码:
require "net/http"
uri = URI.parse("https://www.google.com")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
我的Mac OSX中有两个irb
版本。一个是默认的系统。另一个稍后通过rvm安装。
当使用旧的irb时,运行上面的代码,它给了我:
#<Net::HTTPOK 200 OK readbody=true>
当使用更新的irb时,运行上面的代码,它给了我:
OpenSSL::SSL::SSLError: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed
from /Users/user/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/net/http.rb:923:in `connect'
from /Users/user/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/net/http.rb:923:in `block in connect'
from /Users/user/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/timeout.rb:74:in `timeout'
from /Users/user/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/net/http.rb:923:in `connect'
from /Users/user/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/net/http.rb:863:in `do_start'
from /Users/user/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/net/http.rb:852:in `start'
from /Users/user/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/net/http.rb:1375:in `request'
from (irb):7
from /Users/user/.rvm/rubies/ruby-2.2.1/bin/irb:11:in `<main>'
应该是新版本的ruby无法识别SSL的问题。在StackOverflow上尝试了different solutions,但似乎没有解决问题。
答案 0 :(得分:1)
将我的OSX升级到El Capitan之后,我通过rvm重新安装Ruby 2.2.3,它可以工作。