Reputation: 1965
I've been trying to modify this method from redirecting and returning the contents of the url to returning new valid url instead.
After reading up on the Net::HTTP object, I'm still not sure how exactly the get_response method works. Is this what's downloading the page? is there another method I could call that would just ping the url instead of downloading it?
require 'net/http'
def validate(url)
uri = URI.parse(url)
response = Net::HTTP.get_response(uri)
case response
when Net::HTTPSuccess
return response
when Net::HTTPRedirection
return validate(response['location'])
else
return nill
end
end
puts validate('http://somesite.com/somedir/mypage.html')
Upvotes: 0
Views: 2197
Reputation: 37672
You are correct that get_response
sends an HTTP GET request to the server, which requests the whole page.
You want to use a HEAD request instead of GET. This requests the same HTTP response header that a GET request would get, including the status code (200, 404, etc.), but without downloading the whole page.
See the request_head
and head
methods of Net::HTTP
. For example
url = URI.parse('http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html')
res = Net::HTTP.start(url.host, url.port) {|http|
http.head(url.path)
}
puts res.class
Upvotes: 1
Reputation: 4455
Do you mean, by 'ping the url', you want to know whether the url request returns an HTTP 200 response?
I haven't looked at the implementation of get_response, but I think it just sends out an HTTP GET request, by the looks of it.
If you want to check for the HTTP 200 response, I guess you could just keep doing get_response until you get HTTPSuccess && HTTPOK.
Upvotes: 0