Search code examples
rubywgetopen-uri

open("http://www.google.com') failed but wget works


open failed:

irb(main):001:0> require 'open-uri'
=> true
irb(main):002:0> open("http://www.google.com")
RuntimeError: Non-HTTP proxy URI: 
    from /usr/lib/ruby/1.8/open-uri.rb:203:in `open_http'
    from /usr/lib/ruby/1.8/open-uri.rb:616:in `buffer_open'
    from /usr/lib/ruby/1.8/open-uri.rb:164:in `open_loop'
    from /usr/lib/ruby/1.8/open-uri.rb:162:in `catch'
    from /usr/lib/ruby/1.8/open-uri.rb:162:in `open_loop'
    from /usr/lib/ruby/1.8/open-uri.rb:132:in `open_uri'
    from /usr/lib/ruby/1.8/open-uri.rb:518:in `open'
    from /usr/lib/ruby/1.8/open-uri.rb:30:in `open'
    from (irb):2

But ,wget() works...

root@pierr-desktop:/work/web/yy# wget www.google.com
--2010-11-14 20:00:39--  http://www.google.com/
Resolving www.google.com... 72.14.203.104, 72.14.203.99
Connecting to www.google.com|72.14.203.104|:80... connected.
HTTP request sent, awaiting response... 302 Found
......... 

2010-11-14 20:00:40 (47.7 KB/s) - `index.html' saved [9097]

Do I have to set up the proxy but I don't know the correct proxy information..


Solution

  • Open-URI picks up proxy from the environment if it's set there, or you can define it when you open the connection, or even disable proxy if it is set in the environment:

    # The environment variables such as http_proxy, https_proxy and ftp_proxy
    # are in effect by default.  :proxy => nil disables proxy.
    
    open("http://www.ruby-lang.org/en/raa.html", :proxy => nil) {|f|
      # ...
    }
    

    See Open-URI Documentation

    Also, your Ruby appears to be old: /usr/lib/ruby/1.8/. Do ruby -v and if it's not at least 1.8.7 I'd recommend installing RVM, then using it to install a current Ruby (1.9.2) or at least the latest 1.8 version (1.8.7). Check the RVM Prerequisites page, then follow the installation directions including the parts about modifying your account's start-up script, and then read the part about installing Ruby gems.

    I don't recommend using a packaged Ruby via yum or apt. They aren't going to be recent and seem to be missing all the creature comforts we expect from a stock Ruby source installation. RVM makes it so easy to install and manage multiple rubies.