Forum: Ruby Get Status Code of URL

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Desingurajan A. (Guest)
on 2008-12-18 17:13
  url = URI.parse("http://www.google.com/")
  @req = Net::HTTP.new(url.host)
  @req.start() do |req|
      x = req.head(url.path)
        print x
  end

It will print
#<Net::HTTPFound:0xe70e30>

From Object x i am not able to get only the result (ie. Not found)
Any methods associate with x to get status code alone.

If there is Some other way to get status code of the url, then also
please reply me...
Scott Lillibridge (Guest)
on 2008-12-18 17:55
(Received via mailing list)
Net::HTTP#start doesn't do the request

 require 'net/http'
 url = URI.parse("http://www.google.com")
 req = Net::HTTP.new(url.host)
 req.request_get("/") do |res|
  puts res.code
 end

On Thu, Dec 18, 2008 at 8:06 AM, Desingurajan Aswanthaaman <
Desingurajan A. (Guest)
on 2008-12-18 18:51
Scott Lillibridge wrote:
> Net::HTTP#start doesn't do the request
>
>  require 'net/http'
>  url = URI.parse("http://www.google.com")
>  req = Net::HTTP.new(url.host)
>  req.request_get("/") do |res|
>   puts res.code
>  end
>
> On Thu, Dec 18, 2008 at 8:06 AM, Desingurajan Aswanthaaman <

Thank you
Desingurajan A. (Guest)
on 2008-12-19 03:37
May be helpful, Only requesting header

    require 'net/http'

    url = URI.parse("http://www.google.com/")
    req = Net::HTTP.new(url.host)
    req.start() do |req|
    x = req.head(url.path)
  puts x.code
  puts x.message
  x.each {|key, val| print key," = ",val,"\n" }
    end
This topic is locked and can not be replied to.