OpenSSL::SSL::SSLError: SSL_connect error on Ruby

2019-08-09 14:59发布

问题:

Here's the code to run:

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)

I got two version of irb in my Mac OSX. One comes default with system. The other is installed later on through rvm.

  • /usr/bin/irb (ruby 2.0.0p481)
  • /Users/user/.rvm/rubies/ruby-2.2.1/bin/irb (ruby 2.2.1p85)

When using older irb, run above code, it gives me:

#<Net::HTTPOK 200 OK readbody=true>

When using newer irb, run above code, it gives me:

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>'

It should be the issue of SSL is not recognized by new version of ruby. Tried different solutions on StackOverflow but none of them seems fix the problem.

回答1:

After upgrade my OSX to El Capitan, I reinstall Ruby 2.2.3 through rvm, and it works.



标签: ruby macos ssl