[Rails]驗證網址是否存在
#加入function
def remote_file_exists?(url)
url = URI.parse(url)
Net::HTTP.start(url.host, url.port) do |http|
return http.head(url.request_uri).code == "200"
end
end
測試
puts remote_file_exists? 'http://www.www.www' # => false
puts remote_file_exists? 'http://www.google.com/' # => true
留言