I want to be abl开发者_如何学JAVAe to survey a website and see if it is online. I would like it to return a boolean value, but i can't find out a way to do it. Thanks.
Since @JacobRelkin yanked his answer, I'll post it. Note that you must include the protocol in the domain name.
require 'net/http'
require 'uri'
def website_online?(site_url)
begin
url = URI.parse(site_url)
res = Net::HTTP.start(url.host, url.port) {|http|
http.get('/')
}
res.body.length > 0
rescue
false
end
end
p website_online?( 'phrogz.net' )
#=> false
p website_online?( 'http://phrogz.net' )
#=> true
Varies what you mean by "online" and also if ping isn't disabled.
system('ping hostname')
You also get access to all of the built in system tools feedback.
精彩评论