I'm making a HTTPS call, debugging as the following. open_uri works fine:
irb(main):053:0* r=OpenURI::open_uri("https://192.168.8.123/api/1.0/appliance-management/components/component/status", opts)
=> #<StringIO:0x007fc6e4a8ee38 @base_uri=#<URI::HTTPS https://192.168.8.123/api/1.0/appliance-management/components/component/status>, @meta={"cache-control"=>"private", "expires"=>"Thu, 01 Jan 1970 00:00:00 GMT", "set-cookie"=>"JSESSIONID=B117FD42DA5509EBF99627C08332F249; Path=/; Secure; HttpOnly", "content-type"=>"application/json", "transfer-encoding"=>"chunked", "date"=>"Fri, 15 Apr 2016 14:16:08 GMT", "server"=>""}, @metas={"cache-control"=>["private"], "expires"=>["Thu, 01 Jan 1970 00:00:00 GMT"], "set-cookie"=>["JSESSIONID=B117FD42DA5509EBF99627C08332F249; Path=/; Secure; HttpOnly"], "content-type"=>["application/json"], "transfer-encoding"=>["chunked"], "date"=>["Fri, 15 Apr 2016 14:16:08 GMT"], "server"=>[""]}, @status=["200", "OK"]>
But Net::HTTP always gets ReadTimeout error:
irb(main):096:0* http = Net::HTTP.new('192.168.8.123', 443)
=> #<Net::HTTP 192.168.8.123:443 open=false>
irb(main):097:0> http.use_ssl = true
=> true
irb(main):099:0> http.verify_mode=OpenSSL::SSL::VERIFY_NONE
=> 0
irb(main):102:0* req = Net::HTTP::Get.new("/api/1.0/appliance-management/components/component/status", {})
=> #<Net::HTTP::Get GET>
irb(main):104:0* req.basic_auth('admin', 'default')
=> ["Basic YWRtaW46ZGVmYXVsdA=="]
irb(main):106:0* http.request(req)
Net::ReadTimeout: Net::ReadTimeout
from /opt/ruby/2.2.0/lib/ruby/2.2.0/net/protocol.rb:158:in `rescue in rbuf_fill'
What's wrong with my Net::HTTP code?
答案 0 :(得分:0)
我已经想到了这个问题。那是因为Ruby 2.2的变化。在Ruby 1.9.1中,Net :: HTTP默认不使用代理,但在Ruby 2.2中,Net :: HTTP默认从ENV获取代理。因此,在Ruby 2.2中,当我们初始化Net :: HTTP对象时,我们必须明确强制不使用代理:
http = Net::HTTP.new(ip, port, nil)