HTTPS request using Net::HTTP's block form — is it possible?

白昼怎懂夜的黑 提交于 2019-12-01 01:31:41

问题


To do a Net::HTTP https request without the block form you can do this:

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

But is there a way to tell Net::HTTP to use https when doing the block form?

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

I'm on ruby 1.8.7


回答1:


See the documentation for Net::HTTP.start which takes an optional hash. From the documentation:

opt sets following values by its accessor. The keys are 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 and verify_mode. If you set :use_ssl as true, you can use https and default value of verify_mode is set as OpenSSL::SSL::VERIFY_PEER.

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


来源:https://stackoverflow.com/questions/6347187/https-request-using-nethttps-block-form-is-it-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!