在HTTP POST SSL错误(未知协议)(SSL Error on HTTP POST (Unk

2019-08-19 22:43发布

试图通过SSL连接到Imgur API给我一个错误。 下面的代码和错误:

  API_URI = URI.parse('https://api.imgur.com')
  API_PUBLIC_KEY = 'Client-ID --'

  ENDPOINTS = {
    :image   => '/3/image',
    :gallery => '/3/gallery'
  }

  # Public: Upload an image
  #
  # args    - The image path for the image to upload
  # 
  def upload(image_path)
    http = Net::HTTP.new(API_URI.host)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE

    params   = {'image' => File.open(image_path)}
    request  = Net::HTTP::Post.new(API_URI.request_uri)
    request.set_form_data(params)
    request.add_field('Authorization', API_PUBLIC_KEY)

    response = http.request(request)
    puts response.body
  end

和错误:

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

我知道VERIFY_NODE是不好的做法,但我只是想测试一下现在的连接。

红宝石版本:1.9.2

Answer 1:

创建固定这个问题的HTTP客户端时指定的端口。

http = Net::HTTP.new(API_URI.host, API_URI.port)

要么

http = Net::HTTP.new(API_URI.host, 443)


Answer 2:

对我来说,那是因为我已经开始将服务器作为HTTP(TCP://)服务器,而不是HTTPS(SSL://)。

bundle exec puma config.ru -b 'tcp://0.0.0.0:3456?key=/path/to/key.key&cert=/path/to/cert.crt'

代替:

bundle exec puma config.ru -b 'ssl://0.0.0.0:3456?key=/path/to/key.key&cert=/path/to/cert.crt'


文章来源: SSL Error on HTTP POST (Unknown Protocol)
标签: ruby api ssl https