Ruby:检查 URI 是否为 HTTPS?
我想检查 URI 是否需要 SSL 身份验证:
url = URI.parse("http://www.google.com")
# [some code]
if url.instance_of? URI::HTTPS
http.use_ssl=true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
但是,这几行会抛出以下错误。
/usr/lib/ruby/1.8/uri/common.rb:436:in `split': bad URI(is not URI?): HTTPS (URI::InvalidURIError)
from /usr/lib/ruby/1.8/uri/common.rb:485:in `parse'
from /usr/lib/ruby/1.8/uri/common.rb:608:in `URI'
from links.rb:18
为什么会发生这种情况?
I would like to check if the URI will need SSL authentication:
url = URI.parse("http://www.google.com")
# [some code]
if url.instance_of? URI::HTTPS
http.use_ssl=true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
However, those few lines throw the following error..
/usr/lib/ruby/1.8/uri/common.rb:436:in `split': bad URI(is not URI?): HTTPS (URI::InvalidURIError)
from /usr/lib/ruby/1.8/uri/common.rb:485:in `parse'
from /usr/lib/ruby/1.8/uri/common.rb:608:in `URI'
from links.rb:18
Why is it happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,您可以根据简单的“https”字符串检查 uri 的方案。
So you could check uri's scheme against simple "https" string.
如上一个答案所示,
HTTP
和HTTPS
是不同的类。特别是,
HTTPS
是HTTP
类的子类。因此您可以使用instance_of?
进行检查。但如果这个层次结构发生改变,那么你的代码可能会崩溃,因此上面的答案可能更适合未来。
as shown in the previous answer,
HTTP
andHTTPS
are different classes.in particular,
HTTPS
is a subclass of theHTTP
class. thus you could check withinstance_of?
.but if this hierarchy ever gets changed, then your code could break, thus the above answer might be more future-proof.