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 weir开发者_运维技巧d 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.
Given a recognised scheme (e.g. http/https), a more specialised class will be instantiated. Otherwise a "generic" URI is created; the idea of a "request URI" only makes sense for some URI schemes.
Example:
irb(main):001:0> require 'uri'
=> true
irb(main):002:0> URI.parse('http://www.google.com/').class
=> URI::HTTP
irb(main):003:0> URI.parse('https://www.google.com/').class
=> URI::HTTPS
irb(main):004:0> URI.parse('foo://www.google.com/').class
=> URI::Generic
irb(main):005:0> URI.parse('http://www.google.com/').respond_to?(:request_uri)
=> true
irb(main):006:0> URI.parse('https://www.google.com/').respond_to?(:request_uri)
=> true
irb(main):007:0> URI.parse('foo://www.google.com/').respond_to?(:request_uri)
=> false
So one of the URIs you're parsing has a strange scheme -- even though it's a valid URI -- that's all.
First off, as to why it's #
, my guess is that this error is appearing in the browser and is therefore being considered HTML. Try viewing source and seeing if it's not #<ObjectWhatever>
.
As for the error, that sounds correct. It just means that the URI object you're using has no request_uri
method. Maybe you meant to use path
?
精彩评论