Ruby无法发送SSL请求,但Curl可以

时间:2014-07-18 10:12:59

标签: ruby-on-rails ruby curl ssl

我在Ruby中遇到了错误。但它适用于CURL

环境为rbenv

Ruby版本:ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]

error: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed

卷曲

curl -k https://xx:443/ -i \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
-X POST -d @${1} https://xx:443/service/

红宝石

@uri = URI.parse(server_url)
@https = Net::HTTP.new(@uri.host, @uri.port)
@https.use_ssl = true

req = Net::HTTP::Post.new(@uri.path, initheader = @header )
to_send_body = {
  "ServiceID" => "SignUp",
  "UserID" => "fs.test@dsd.com",
  "Password" => "123456789"
}
req.body = to_send_body.to_json
res = @https.request(req)

data.json

{"ServiceID" : "SignUp","UserID" : "poc.test@123.com","Password" : "123456789"}

更新

我终于通过curb gem完成了它,但仍然以http方式失败,

路缘版

c = Curl::Easy.http_post("https://1xx.xx.x.2:443/service/", to_send_body.to_json   
    ) do |curl|
      curl.ssl_verify_peer = false
      curl.headers['Accept'] = 'application/json'
      curl.headers['Content-Type'] = 'application/json'
    end        

1 个答案:

答案 0 :(得分:1)

curl中的-k标志表示当证书无效时它仍将连接。要在ruby中完成相同的操作,请使用以下

@https = Net::HTTP.new(@uri.host, @uri.port)
@https.use_ssl = true
@https.verify_mode = OpenSSL::SSL::VERIFY_NONE