使用 Net::HTTP.get 获取 https 网址 [英] Using Net::HTTP.get for an https url
本文介绍了使用 Net::HTTP.get 获取 https 网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将 Net::HTTP.get()
用于 https URL:
I'm trying to use Net::HTTP.get()
for an https URL:
@data = Net::HTTP.get(uri, Net::HTTP.https_default_port())
但是,当我尝试打印结果时得到以下结果:
However, I get the following result when I try to print the results:
无法将 URI::HTTPS 转换为字符串
can't convert URI::HTTPS into String
怎么了?我使用的是 Ruby 1.8.7 (OS X)
What's the deal? I'm using Ruby 1.8.7 (OS X)
推荐答案
原答案:
uri = URI.parse("https://example.com/some/path")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
@data = http.get(uri.request_uri)
正如评论中所指出的,这更优雅:
As pointed out in the comments, this is more elegant:
require "open-uri"
@data = URI.parse("https://example.com/some/path").read
这篇关于使用 Net::HTTP.get 获取 https 网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文