有没有一种解决方法可以在Ruby中打开包含下划线的URL? [英] Is there a workaround to open URLs containing underscores in Ruby?
本文介绍了有没有一种解决方法可以在Ruby中打开包含下划线的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用open-uri打开URL.
I'm using open-uri to open URLs.
resp = open("http://sub_domain.domain.com")
如果其中包含下划线,则会出现错误:
If it contains underscore I get an error:
URI::InvalidURIError: the scheme http does not accept registry part: sub_domain.domain.com (or bad hostname?)
我了解这是因为根据RFC,URL只能包含字母和数字.有什么解决方法吗?
I understand that this is because according to RFC URLs can contain only letters and numbers. Is there any workaround?
推荐答案
这看起来像URI中的错误,并且uri-open,HTTParty和许多其他gem都使用URI.parse.
This looks like a bug in URI, and uri-open, HTTParty and many other gems make use of 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中打开包含下划线的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文