使用Net :: HTTP的块形式的HTTPS请求 - 是否可能?

时间:2011-06-14 16:55:55

标签: ruby ssl https net-http

要执行不带阻止表单的Net :: HTTP https请求,您可以执行此操作:

...
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
...

但有没有办法告诉Net :: HTTP在执行阻止表单时使用https?

u = URI.parse(url)
Net::HTTP.start(u.host, u.port) do |http|
  # if I put http.use_ssl = true here, ruby complains that this can't
  # be done becuase the sesion has already started
  resp = http.get(u.request_uri)
end

我正在使用ruby 1.8.7

1 个答案:

答案 0 :(得分:19)

请参阅Net::HTTP.start的文档,其中包含可选的哈希值。来自文档:

  

opt由其访问者设置以下值。密钥是ca_file,ca_path,cert,cert_store,ciphers,close_on_empty_response,key,open_timeout,read_timeout,ssl_timeout,ssl_version, use_ssl ,verify_callback,verify_depth和verify_mode。如果将:use_ssl设置为true,则可以使用https,并将verify_mode的默认值设置为OpenSSL :: SSL :: VERIFY_PEER。

Net::HTTP.start(url.host, url.port, :use_ssl => true)