LoginSignup
14
9

More than 5 years have passed since last update.

RubyでHTTPステータスコードを確認する方法

Last updated at Posted at 2015-05-06

必要なクラスとモジュール

  1. Net::HTTP
  2. URI

ステータスコードを確認する

以下のコードで文字列でステータスコードが返却される

ruby
Net::HTTP.get_response(URI.parse(site.uri)).code

タイムアウトも付けたい

ruby
TIME_OUT = 10 # 10秒でタイムアウト
status = timeout(TIME_OUT){
  Net::HTTP.get_response(URI.parse(site.uri)).code
}
14
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
9