我正在使用 open-uri 打开 URL。
resp = open("http://sub_domain.domain.com")
如果它包含下划线,我会得到一个错误:
URI::InvalidURIError: the scheme http does not accept registry part: sub_domain.domain.com (or bad hostname?)
据我所知,这是因为根据 RFC,URL 只能包含字母和数字。有什么解决方法吗?
最佳答案
这看起来像是 URI 中的错误,而 uri-open、HTTParty 和许多其他 gem 都使用了 URI.parse。
解决方法如下:
require 'net/http'
require 'open-uri'
def hopen(url)
begin
open(url)
rescue URI::InvalidURIError
host = url.match(".+\:\/\/([^\/]+)")[1]
path = url.partition(host)[2] || "/"
Net::HTTP.get host, path
end
end
resp = hopen("http://dear_raed.blogspot.com/2009_01_01_archive.html")
关于ruby - 在 Ruby 中是否有打开包含下划线的 URL 的解决方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5208851/