嘿 - 我正在编写一个使用digg API的基本Rails应用程序。我正在尝试解析digg的api为hpricot提供的xml数据,但在测试页面时,浏览器会挂起,直到我最终捕获到Timeout :: Error异常。
这是控制器的代码:
require 'rubygems'
require 'hpricot'
require 'open-uri'
appkey = 'http://mportiz08.homeip.net/twigg'
query = CGI::escape(params[:id].gsub('_', ' ').gsub('#', ''))
@request = 'http://services.digg.com/search/stories?query=' + query + '&appkey=' + appkey
@response = Hpricot( open(@request) )
这是堆栈跟踪:
/usr/lib/ruby/1.8/timeout.rb:60:in `rbuf_fill'
/usr/lib/ruby/1.8/net/protocol.rb:134:in `rbuf_fill'
/usr/lib/ruby/1.8/net/protocol.rb:116:in `readuntil'
/usr/lib/ruby/1.8/net/protocol.rb:126:in `readline'
/usr/lib/ruby/1.8/net/http.rb:2020:in `read_status_line'
/usr/lib/ruby/1.8/net/http.rb:2009:in `read_new'
/usr/lib/ruby/1.8/net/http.rb:1050:in `request'
/usr/lib/ruby/1.8/open-uri.rb:248:in `open_http'
/usr/lib/ruby/1.8/net/http.rb:543:in `start'
/usr/lib/ruby/1.8/open-uri.rb:242:in `open_http'
/usr/lib/ruby/1.8/open-uri.rb:616:in `buffer_open'
/usr/lib/ruby/1.8/open-uri.rb:164:in `open_loop'
/usr/lib/ruby/1.8/open-uri.rb:162:in `catch'
/usr/lib/ruby/1.8/open-uri.rb:162:in `open_loop'
/usr/lib/ruby/1.8/open-uri.rb:132:in `open_uri'
/usr/lib/ruby/1.8/open-uri.rb:518:in `open'
/usr/lib/ruby/1.8/open-uri.rb:30:in `open'
/home/marcus/dev/ruby/twigg/app/controllers/stories_controller.rb:15:in `view'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:1327:in `send'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:1327:in `perform_action_without_filters'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/filters.rb:617:in `call_filters'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/filters.rb:610:in `perform_action_without_benchmark'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/rescue.rb:160:in `perform_action_without_flash'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/flash.rb:146:in `perform_action'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:527:in `send'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:527:in `process_without_filters'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/filters.rb:606:in `process'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:391:in `process'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/base.rb:386:in `call'
/var/lib/gems/1.8/gems/actionpack-2.3.3/lib/action_controller/routing/route_set.rb:434:in `call'
我刚刚学习Rails,我无法弄清楚发生了什么。关于为什么会发生这种情况的任何想法?
更新
我使用本地保存的xml文件尝试了相同的完整过程,并且它工作得很好 - 问题可能与open-uri和远程xml有关
答案 0 :(得分:1)
Mayb diggs只接受webbrowser?
我认为HPricot会发送另一个用户标题? 最好找出从hpricot发送的标题?
答案 1 :(得分:0)
Lichtamberg - 你是对的。问题是digg需要使用请求发送用户代理标头:see here
我刚修改了这样的open方法调用:
@response = Hpricot( open(@request, 'User-Agent' => 'twigg') )