(rails) url 验证的奇怪问题
我正在尝试查看某个网址是否存在。这是我这样做的代码:
validate :registered_domain_name_exists
private
def registered_domain_name_exists
if url and url.match(URI::regexp(%w(http https))) then
begin # check header response
case Net::HTTP.get_response(URI.parse(url))
when Net::HTTPSuccess then true
else errors.add(:url, "URL does not exist") and false
end
rescue # DNS failures
errors.add(:url, "URL does not exist") and false
end
end
end
但是,该代码失败了。它说 http://www.biorad.com 不是一个有效的网站。这是绝对不正确的。另外,知道 http://www.biorad.com 只是将您重定向到 http://www.bio-rad.com/evportal/evolutionPortal.portal 我也尝试了这个网址,那也失败了。再说一次,我知道这是不可能的。我的代码有什么问题吗?
i'm trying to see if a url exists. here is my code for doing so:
validate :registered_domain_name_exists
private
def registered_domain_name_exists
if url and url.match(URI::regexp(%w(http https))) then
begin # check header response
case Net::HTTP.get_response(URI.parse(url))
when Net::HTTPSuccess then true
else errors.add(:url, "URL does not exist") and false
end
rescue # DNS failures
errors.add(:url, "URL does not exist") and false
end
end
end
however, this code is failing. it says http://www.biorad.com is not a valid website. this is absolutely incorrect. Also, knowing that http://www.biorad.com just redirects you to http://www.bio-rad.com/evportal/evolutionPortal.portal i tried this url too, and that also failed. again, i know this can't be possible. what's wrong with my code??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您提供的每个示例 url 都是重定向(http 状态代码 301 或 302)。您的代码仅将 http 状态代码 2xx 视为成功。添加另一种情况:
更新:请注意,使用 HTTP HEAD 而不是 GET 将通过网络传输更少的数据。
Each of the example urls you gave is a redirect (http status code 301 or 302). Your code is only considering http status code 2xx to be success. Add another case:
UPDATE: Note that using HTTP HEAD instead of GET will transmit less data across the network.