OpenURI::HTTPError
有io
属性,你可以检查得到你想要的东西。 io
是StringIO
物体(status
例如)在其上定义了几种特殊方法:
require 'open-uri'
begin
open('http://www.google.co.uk/sorry/?continue=http://www.google.co.uk/search%3Fq%3Dhello%26oq%3Dhello%26ie%3DUTF-8')
rescue OpenURI::HTTPError => error
response = error.io
response.status
# => ["503", "Service Unavailable"]
response.string
# => <!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n<html DIR=\"LTR\">\n<head><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"><meta name=\"viewport\" content=\"initial-scale=1\">...
end
但是此任务的Net::HTTP
模块可能是一个更好的选择:
require 'net/http'
response = Net::HTTP.get_response(URI.parse('http://www.google.co.uk/sorry/?continue=http://www.google.co.uk/search%3Fq%3Dhello%26oq%3Dhello%26ie%3DUTF-8'))
response.code
# => "503"
response.body
# => "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n<html DIR=\"LTR\">\n<head><meta http-equiv=\"content-type\" content=\"text/html; ...
谢谢!在您编辑之前,我已阅读了您对该问题的回答,我正在阅读有关例外情况的内容,并且不应将其用于常见情况。那么最好是使用net/http而不是open-uri,因为这意味着我不必使用它。 –