看过:Escapeing parameters in set_form_data POST和Ruby on Rails HTTPS Post Bad Request
def send_request(params)
host = "https://hc.mercurydev.net"
uri = URI.parse(host)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
xml = build_xml_for_checkout(params)
http.start do |http|
req = Net::HTTP::Post.new('http://www.mercurypay.com/InitializePayment')
header = {'Host' => 'hc.mercurydev.net', 'Content-Type' => 'text/xml; charset=utf-8', 'Content-Length' => 'length', 'SOAPAction' => 'http://www.mercurypay.com/InitializePayment'}
req.set_form_data(header, xml)
response = http.request(req)
end
end
这是我第一次不得不在 ruby 中构建自己的发布请求,我确信我缺少一些简单的东西,但是什么?
-- 我已经确认 xml 是 100% 好的,所以它必须在我的标题中或在我构建请求的方式中。
编辑:看了ruby-api-doc之后:我想出了这个:
uri = URI.parse(host)
Net::HTTP.start(uri.hostname, uri.port,
:use_ssl => uri.scheme == 'https').start do |http|
req = Net::HTTP::Post.new uri.path
req.body = xml
req.set_form_data(header)
res = http.request req
end
它返回(即使我重新启动服务器)我有一个打开的 HTTP 会话。