1

我正在尝试使用 Rails 控制器发送发布请求,但是当我无法让控制器接受 use_ssl = true 时,我遇到了问题。这是我的代码

require "net/http"
require "uri"

params = {'requestId' => '1', 
  'merchantId' => 'merchantid'
  'version' => '1.0.6', 
  'locale' => 'en_US', 
  'externalUserId' => '1234', 
  'partnerId' => 'partnerid', 
  'emailAddress' => 'test@email.com', 
  'deviceDTO' => "{'id' => '123123213', 'deviceType' => 'samsung'}",
  'timestamp' => Time.now.strftime('%Y-%m-%dT%H:%M:%S.%L%z')
}

url = URI.parse('https://example.com')
http = Net::HTTP::Post.new(url.host)
http.set_form_data(params)
#http.use_ssl = true
#http.verify_mode = OpenSSL::SSL::VERIFY_PEER


res = Net::HTTP.start(url.hostname, url.port) do |h|
  h.request(http)
  h.use_ssl = true
  h.verify_mode = OpenSSL::SSL::VERIFY_NONE
end

错误是:

NoMethodError: undefined method `use_ssl=' for #<Net::HTTP::Post POST>
NoMethodError: undefined method `verify_mode=' for #<Net::HTTP::Post POST>

现在 UPD 错误

Errno::ECONNRESET: Connection reset by peer
4

1 回答 1

2

use_ssl是一个方法Net::HTTP,你正在创建一个Net::HTTP::Post对象。

你可能想做

url = URI.parse('https://example.com/')
http = Net::HTTP.new(url.host, url.port)
if url.scheme == 'https'
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
end
http.post(url.path, params)
于 2012-11-20T08:23:21.157 回答