How to connect to an https server with an invalid

2019-04-03 04:04发布

Consider the following code:

require 'net/https'
uri = URI.parse("https://host/index.html")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.path)
response = http.request(request)

where https://host/index.html is a valid address with an invalid certificate on the server.

On older versions of ruby (specifically 1.8.7-p334 and 1.9.2-p180) this code works fine. On all recent versions (1.8.7-p352, 1.9.2-p290 and 1.9.3-p0) it throws the following exception:

OpenSSL::SSL::SSLError: SSL_connect SYSCALL returned=5 errno=0 state=unknown state

on the last line.

Changing verify_mode to OpenSSL::SSL::VERIFY_PEER gives exactly the error suggesting it is attempting to verify the certificate in spite of the setting.

How can I convince ruby to ignore the invalid certificate and connect?

标签: ruby ssl
2条回答
beautiful°
2楼-- · 2019-04-03 04:15
require 'nokogiri'
require 'open-uri'
require 'net/https'

@doc = Nokogiri::HTML(open(my_url,  :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE))
查看更多
地球回转人心会变
3楼-- · 2019-04-03 04:16
uri = URI("https://host/index.html")
req = Net::HTTP::Get.new(uri.path)

res = Net::HTTP.start(
        uri.host, uri.port, 
        :use_ssl => uri.scheme == 'https', 
        :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |https|
  https.request(req)
end
查看更多
登录 后发表回答