在rails ssl post上从ruby获得错误请求

时间:2012-10-11 16:28:56

标签: ruby-on-rails-3 post https xmlhttprequest httpresponse

注意:Escaping parameters in set_form_data POSTRuby 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会话返回(即使我重新启动我的服务器)。

1 个答案:

答案 0 :(得分:3)

最终结果如下:

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

req = Net::HTTP::Post.new(uri.request_uri)
req.body = xml.to_s
req.initialize_http_header(header)

response = http.request(req)

我失踪的关键是initialize_http_header()一旦我得到了那个,肥皂api接受了我的提交,然后只是确保我的语法正确的问题。