LoginSignup
3
2

More than 3 years have passed since last update.

OpenSSL::SSL::SSLError (SSL_connect returned=1 errno=0 state=SSLv2/v3 read server hello A: unknown protocol) の対処法

Last updated at Posted at 2019-06-14

コンパスAPIからAPIを引っ張ってくる時に、以下のエラーが頻出してどツボにハマったので、二度とはまらないように脱出方法をメモ。

OpenSSL::SSL::SSLError (SSL_connect returned=1 errno=0 state=SSLv2/v3 read server hello A: unknown protocol)

エラーコード

require 'net/http'
require 'uri'
require 'json'

$ uri = Addressable::URI.parse("https://connpass.com/api/v1/event/?keyword=#名古屋&ym=201907,201908&count=30&order=5")

$ http = Net::HTTP.new(uri.host, uri.port)
$ http.use_ssl = true
$ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
$ http.open_timeout = 5
$ http.read_timeout = 10
$ response = http.get(uri.request_uri)

実行すると、

OpenSSL::SSL::SSLError (SSL_connect returned=1 errno=0 state=SSLv2/v3 read server hello A: unknown protocol)

というエラーが発生。

色々試す

調べてみると、RubyがSSL証明書を見つけることが出来ないがために、SSL接続によるHTTP通信に失敗しているみたい。

そのため、SSL証明書をダウンロードし、

$ http.ca_file = './cacert.pem'

にて、CA証明書ファイルのパスを指定する。

すると、

require 'net/http'
require 'uri'
require 'json'

$ uri = Addressable::URI.parse("https://connpass.com/api/v1/event/?keyword=#名古屋&ym=201907,201908&count=30&order=5")

$ http = Net::HTTP.new(uri.host, uri.port)
$ http.use_ssl = true
$ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
$ http.ca_file = './cacert.pem'
$ http.open_timeout = 5
$ http.read_timeout = 10
$ response = http.get(uri.request_uri)

OpenSSL::SSL::SSLError (SSL_connect returned=1 errno=0 state=SSLv2/v3 read server hello A: unknown protocol)

なぜ...

解決策

$ http = Net::HTTP.new(uri.host, uri.port) # これを
↓
$ http = Net::HTTP.new(uri.host, uri.inferred_port) # に変換

日本語のURIをパースしたかったため、AddressableというGemを使用していたんですが、Net::HTTP.new(uri.host, uri.port)する際に、portではなくinferred_portと明示させなくてはいけないみたい。

三時間ほどの泥沼から脱出。。。

参考ソース

HTTPS doesn't work when using Addressable::URI #318

エラー:OpenSSL::SSL::SSLError SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed

Rubyで日本語のURI(URL)をパーズするなら Addressable::URI を使おう

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2