我正在尝试异步获取多个URL,只有1个URL成功。 请帮助调查以下代码中的原因,并且不理解为什么处理 errback 而不是处理回调?您能指出问题吗?
require 'eventmachine'
require 'em-http-request'
def url_handler(url)
http = EventMachine::HttpRequest.new(url).get
http.errback {
puts "Oops #{url}"
puts '-'*20
EventMachine.stop
}
http.callback {
puts "#{http.response_header.status} #{url}"
# p http.response_header
p http.response[0,60]
puts '-'*20
EventMachine.stop
}
end
urls = [
'https://www.cloudflare.com/robots.txt2', # 404, not found
'http://example.com/',
'https://www.cloudflare.com/robots.txt',
'https://rubygems.org/gems/em-http-request/versions.atom',
'http://twitter.com/robots.txt',
# 'http://localhost/robots.txt',
]
EventMachine.run do
urls.each {|url| url_handler(url) }
end
示例输出:
200 http://example.com/
"<!doctype html>\n<html>\n<head>\n <title>Example Domain</tit"
--------------------
Oops https://www.cloudflare.com/robots.txt2
--------------------
Oops https://www.cloudflare.com/robots.txt
--------------------
Oops https://rubygems.org/gems/em-http-request/versions.atom
--------------------
Oops http://twitter.com/robots.txt
--------------------