在HTTP POST SSL错误(未知协议) [英] SSL Error on HTTP POST (Unknown Protocol)

查看:1198
本文介绍了在HTTP POST SSL错误(未知协议)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图连接到Imgur API通过SSL给我一个错误。这里的code和错误:

Trying to connect to Imgur API via SSL gives me an error. Here's the code and the error:

  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是不好的做法,但我只是想测试对于现在的连接。

I know VERIFY_NODE is not good practice but I just want to test the connection for now.

红宝石版本:1.9.2

Ruby version: 1.9.2

推荐答案

创建HTTP客户端解决了这个问题,当指定的端口。

Specifying the port when creating the HTTP client fixed this problem.

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

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

这篇关于在HTTP POST SSL错误(未知协议)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆