简体   繁体   中英

Check HTTP Status Code in Rails

Is there an easy way to check the status code of another website (eg http://google.com ) in Ruby on Rails?

Ideally, the list of URLs to check would be pulled from a DB and processed via CRON (or equivalent).

require 'net/http'

http = Net::HTTP.new('www.google.com',80)
response = http.request_get('/')
p response.status

Slightly more efficient (traffic wise) might be:

response = http.request_head('/')
p response.status

Source: http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html#M001403

You can access the request env by calling :

def index
  @req = (request.env).inspect 
end

# index.html.erb
<%= @req %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM