Вопрос

I would like to use Ruby's OpenURI to check whether the URL can be properly accessed. So I would like to check its response code (4xx or 5xx means error, etc.) Is it possible to find that?

Это было полезно?

Решение

You can use the status method to return an array that contains the status code and message.

require "open-uri" 

open("http://www.example.org") do |f|
  puts f.base_uri  #=> http://www.example.org
  puts f.status    #=> ["200", "OK"]
end
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top