Question

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??

Was it helpful?

Solution

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:

when Net::HTTPRedirection then true

UPDATE: Note that using HTTP HEAD instead of GET will transmit less data across the network.

uri = URI.parse(url)
response = Net::HTTP.start(uri.host, uri.port) {|http|
  http.head('/')
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top