3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RubyでURIが存在するかどうかチェック

Posted at

URLが存在するかどうかを知りたかったので、URI#exists?を書きました。

.rb
module URI
  def exists?
    req = Net::HTTP.new(host, port)
    req.use_ssl = true if scheme == 'https'
    res = req.request_head(path)
    return URI(res['location']).exists? if %w(301 302).include?(res.code)
    res.code == '200'
  end
end

こちらのコードに301,302の場合に追尾する機能を追加したものです。
https://stackoverflow.com/questions/5908017/check-if-url-exists-in-ruby
(stackoverflowの回答にも追加したかったけどcloseされててできなかったので、reopen権限ある人がいたらお願いします。)

3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?