使用Net :: HTTP的块形式的HTTPS请求 - 是否可能? [英] HTTPS request using Net::HTTP's block form -- is it possible?

查看:154
本文介绍了使用Net :: HTTP的块形式的HTTPS请求 - 是否可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要执行不带阻止表格的Net :: HTTP https请求,您可以这样做:

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
...

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

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

我在ruby 1.8.7

I'm on ruby 1.8.7

推荐答案

请参阅 Net的文档: :HTTP.start ,它采用可选的哈希值。从文档中:

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


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。

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)

这篇关于使用Net :: HTTP的块形式的HTTPS请求 - 是否可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆