2016-04-15 66 views
0

我正在進行HTTPS調用,調試如下。 open_uri正常工作:open-uri工作但net :: http,爲什麼?

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"]> 

但網:: HTTP總是得到ReadTimeout錯誤:

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' 

這有什麼錯我的Net :: HTTP代碼?

+0

可以確認192.168.8.123在端口443到達? 'telnet 192.168.8.123 443' 應迴應 '連接到192.168.8.123。 轉義字符是'^]' 如果是這樣 –

+0

正如你所看到的,open_uri的工作原理。 – TieDad

回答

0

我已經想通了這個問題。這是因爲Ruby 2.2的變化。在Ruby 1.9.1中,默認情況下Net :: HTTP不使用代理,但在Ruby 2.2中,Net :: HTTP默認從ENV獲取代理。因此,在紅寶石2.2,當我們初始化的Net :: HTTP對象,我們必須明確強制不使用代理:

http = Net::HTTP.new(ip, port, nil) 
相關問題