I'm trying to use Ruby to load a webpage over HTTP and check what its status code is. My code looks like this:
require "net/http"
@r = Net::HTTP.get_response(URI.parse(myURL))
return @r.code
However, for some URLs (mostly ones pointing to weird stuff like web counters that won't give a proper response) I'm getting an undefined method request_uri for #
exception. I've traced it back to line 380 of http.rb (I'm running Ruby 1.8), where it says:
def HTTP.get_response(uri_or_host, path = nil, port = nil, &block)
if path
host = uri_or_host
new(host, port || HTTP.default_port).start {|http|
return http.request_get(path, &block)
}
else
uri = uri_or_host
new(uri.host, uri.port).start {|http|
return http.request_get(uri.request_uri, &block) <--- LINE 380
}
end
end
I'm quite lost as to what causes this exception. I would expect a URI::InvalidURIError
, but not this.