HTTPSでサーバーに接続
net/httpsを使ってHTTPSでサーバーに接続します。
サーバーの公開鍵証明書を得る
接続する。
サンプルは、「Rubyリファレンスマニュアル - net/https」記載のものとほぼ同じです。「https.ca_file」で、上で取得した接続先サーバーの公開鍵証明書を、信頼するサーバーの公開鍵証明書として指定します。
サンプル:
require 'net/https' https = Net::HTTP.new('<接続先サーバー>', 443) https.use_ssl = true https.ca_file = './base64.cer' # サーバーの公開鍵証明書を指定 https.verify_mode = OpenSSL::SSL::VERIFY_PEER https.verify_depth = 5 https.start { |w| response = w.get('/') puts response.body }
「https.ca_file」を設定しないと、「certificate verify failed」になります。
require 'net/https' https = Net::HTTP.new('<接続先サーバー>', 443) https.use_ssl = true https.verify_mode = OpenSSL::SSL::VERIFY_PEER https.verify_depth = 5 https.start { |w| response = w.get('/') puts response.body }
実行結果:
/usr/lib/ruby/1.8/net/http.rb:586:in `connect': certificate verify failed (OpenSSL::SSL::SSLError) from /usr/lib/ruby/1.8/net/http.rb:586:in `connect' from /usr/lib/ruby/1.8/net/http.rb:553:in `do_start' from /usr/lib/ruby/1.8/net/http.rb:542:in `start' ...