这是我用来从 Apple 的 UK Top 10 rss 提要中获取 xml 数据的代码:
xml = Net::HTTP.get_response(URI.parse('http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topsongs/limit=10/xml')).body
辛纳屈回归Errno::ECONNRESET at / - Connection reset by peer
。我用于此的 RSS url 是从 Apple RSS 生成器生成的。其他国家/地区的 RSS 提要会出现此错误,但此处的默认提要(美国)不会出现此错误。回溯如下:
/usr/lib/ruby/1.9.1/net/protocol.rb in read_nonblock
@rbuf << @io.read_nonblock(BUFSIZE)
/usr/lib/ruby/1.9.1/net/protocol.rb in rbuf_fill
@rbuf << @io.read_nonblock(BUFSIZE)
/usr/lib/ruby/1.9.1/net/protocol.rb in readuntil
rbuf_fill
/usr/lib/ruby/1.9.1/net/protocol.rb in readline
readuntil("\n").chop
/usr/lib/ruby/1.9.1/net/http.rb in read_status_line
str = sock.readline
/usr/lib/ruby/1.9.1/net/http.rb in read_new
httpv, code, msg = read_status_line(sock)
/usr/lib/ruby/1.9.1/net/http.rb in block in transport_request
res = HTTPResponse.read_new(@socket)
/usr/lib/ruby/1.9.1/net/http.rb in catch
res = catch(:response) {
/usr/lib/ruby/1.9.1/net/http.rb in transport_request
res = catch(:response) {
/usr/lib/ruby/1.9.1/net/http.rb in request
res = transport_request(req, &block)
/usr/lib/ruby/1.9.1/net/http.rb in request_get
request(Get.new(path, initheader), &block)
/usr/lib/ruby/1.9.1/net/http.rb in block in get_response
return http.request_get(uri.request_uri, &block)
/usr/lib/ruby/1.9.1/net/http.rb in start
return yield(self)
/usr/lib/ruby/1.9.1/net/http.rb in get_response
new(uri.hostname, uri.port).start {|http|
/home/sinatra/little.rb in block in <top (required)>
xml = Net::HTTP.get_response(URI.parse('https://itunes.apple.com/gb/rss/topsongs/limit=5/xml')).body
/usr/lib/ruby/1.9.1/webrick/httpserver.rb in service
si.service(req, res)
/usr/lib/ruby/1.9.1/webrick/httpserver.rb in run
server.service(req, res)
/usr/lib/ruby/1.9.1/webrick/server.rb in block in start_thread
block ? block.call(sock) : run(sock)