Check HTTP Status Code in Rails

折月煮酒 提交于 2019-12-10 17:27:16

问题


Is there an easy way to check the status code of another website (e.g. 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).


回答1:


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




回答2:


You can access the request env by calling :

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

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


来源:https://stackoverflow.com/questions/7476813/check-http-status-code-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!