所以我有这个脚本:
#!/usr/bin/ruby
require 'net/https'
require 'open-uri'
puts "HTTPS Client for Ruby!"
puts "Enter the URL"
site = gets.chomp
url = URI.parse(site)
http = Net::HTTP.new(url.host,url.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
http.cert_store = OpenSSL::X509::Store.new
http.cert_store.set_default_paths
http.cert_store.add_file('/home/user/sec/certs/cacert.pem')
page = Net::HTTP.get(url)
puts page
工作正常。它能够获取几乎所有http或https网站的主页的html。但是,我在虚拟机中设置了启用HTTPS的Web服务器,但它无法使用。在我在网络服务器上启用SSL之前,这个脚本很好地抓住了html。所以我的问题是,为什么我会收到这个错误:
/usr/lib/ruby/2.1.0/net/http.rb:920:in `connect': SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (OpenSSL::SSL::SSLError)
from /usr/lib/ruby/2.1.0/net/http.rb:920:in `block in connect'
from /usr/lib/ruby/2.1.0/timeout.rb:67:in `timeout'
from /usr/lib/ruby/2.1.0/net/http.rb:920:in `connect'
from /usr/lib/ruby/2.1.0/net/http.rb:863:in `do_start'
from /usr/lib/ruby/2.1.0/net/http.rb:852:in `start'
from /usr/lib/ruby/2.1.0/net/http.rb:583:in `start'
from /usr/lib/ruby/2.1.0/net/http.rb:478:in `get_response'
from /usr/lib/ruby/2.1.0/net/http.rb:455:in `get'
from https_client.rb:20:in `<main>'
运行脚本试图抓取我的网络服务器的html?我指定的路径在那里有一个实际的证书。
答案 0 :(得分:-1)
当SSL证书是自签名/不是来自经过验证的ssl提供商时,您会收到该错误。因此,假设您自己/您信任该网站并且没有经过验证的证书,请更改
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
到
http.verify_mode = OpenSSL::SSL::VERIFY_NONE