require 'net/http'
require 'uri'
Net::HTTP.get_print URI.parse('http://www.example.com/index.html')当我试图运行这个程序时,我得到了这样的错误,为什么??
c:/ruby/lib/ruby/1.8/net/http.rb:560:in `initialize': getaddrinfo: no address associated with hostname. (SocketError)
from c:/ruby/lib/ruby/1.8/net/http.rb:560:in `open'
from c:/ruby/lib/ruby/1.8/net/http.rb:560:in `connect'
from c:/ruby/lib/ruby/1.8/timeout.rb:48:in `timeout'
from c:/ruby/lib/ruby/1.8/timeout.rb:76:in `timeout'
from c:/ruby/lib/ruby/1.8/net/http.rb:560:in `connect'
from c:/ruby/lib/ruby/1.8/net/http.rb:553:in `do_start'
from c:/ruby/lib/ruby/1.8/net/http.rb:542:in `start'
from c:/ruby/lib/ruby/1.8/net/http.rb:379:in `get_response'
from c:/ruby/lib/ruby/1.8/net/http.rb:337:in `get_print'
from ruby2.rb:3发布于 2010-08-03 21:07:00
代码看起来正确,并在我的机器上按预期运行-你确定dns在你的机器上设置正确吗?您需要代理服务器才能进行web呼叫吗?你能在浏览器中打开url吗?
发布于 2010-08-03 21:07:07
我没有,我得到了一个回应。你当时在线吗?
发布于 2011-01-05 12:05:12
尝试对问题进行三角测量。您可以通过浏览器访问站点,但不能通过代码访问:
从命令行窗口运行
gem server运行一个很好的小型web服务器。http://localhost:8808打开一个指向gem服务器的浏览器窗口。您应该会看到一个格式良好的页面。<CNTRL>+C杀死gem服务器。在这一点上,您已经证明了Net::HTTP在本地工作,并且您已经完成了工作,除了找出机器外部阻止您看到"example.com".require 'open-uri'添加到您的要求列表中,并将HTTP::Net行替换为puts open('http://localhost:8808').read,然后再次尝试运行代码。在这一点上,您将了解到足够多的信息,可以通过编辑将更多细节添加到原始问题中。
另一个想法浮现在脑海中:你使用的是Windows。您是否启用了任何网络安全功能,如防火墙?会不会是它阻止了Ruby创建连接,但却允许你的浏览器?
https://stackoverflow.com/questions/3396695
复制相似问题