0
我有一些代碼檢查URL列表的響應代碼並將它們顯示回來 - 我遇到了一些導致應用程序無法掛起的URL的問題負載。如何讓請求在30秒後放棄,並檢查標記跳過的URL的下一個URL爲失敗。net/http掛起請求導致失敗 - RoR
下面是我目前的代碼; (型號/ status.rb)
require "net/http"
require "uri"
class Status
def initialize(url)
@url = url
end
def get_status
response.code
end
def active?
["200","203","302"].include?(get_status) ? true : false
end
private
def lookup
URI.parse(@url)
end
def http
Net::HTTP.new(lookup.host, lookup.port)
end
def request
Net::HTTP::Get.new(lookup.request_uri)
end
def response
http.request(request)
end
end
(控制器/ welcome_controller.rb)
class WelcomeController < ApplicationController
def index
@syndication = [
["http://v1.syndication.u01.example.uk/organisations?apikey=bbccdd", "U.INT 01"],
["http://v1.syndication.u02.example.uk/organisations?apikey=bbccdd", "U.INT 02"],
].collect { |url| logger.info("Boom #{url[0]}"); ["#{url[1]} (#{url[0]})", Status.new(url[0]).active?] }
end
end
嘗試在請求前將'http.read_timeout = 30'放入'response'方法中。 –