Ruby HTTP与REST客户端gem:它们如何处理坏网站/404
我试图使用rest客户端gem访问一些网站,但我发现了一个令我困惑的行为。这与将rest客户端用于坏网站有关,在这种情况下,www.google.com/this\u不存在 我所期望的:代码将运行,响应对象将具有404响应代码 实际发生的情况:出现异常,代码提前终止 当我在Net::HTTP库中尝试同样的方法时,我确实得到了预期的结果 问题是:rest客户端中是否会出现这种行为?如果是这样的话,当你在坏网站上使用时,你将如何找回一个404响应代码的对象 以下是我的irb代码:Ruby HTTP与REST客户端gem:它们如何处理坏网站/404,ruby,rest-client,net-http,Ruby,Rest Client,Net Http,我试图使用rest客户端gem访问一些网站,但我发现了一个令我困惑的行为。这与将rest客户端用于坏网站有关,在这种情况下,www.google.com/this\u不存在 我所期望的:代码将运行,响应对象将具有404响应代码 实际发生的情况:出现异常,代码提前终止 当我在Net::HTTP库中尝试同样的方法时,我确实得到了预期的结果 问题是:rest客户端中是否会出现这种行为?如果是这样的话,当你在坏网站上使用时,你将如何找回一个404响应代码的对象 以下是我的irb代码: 2.2.1 :04
2.2.1 :045 > uri = URI('http://www.google.com')
=> #<URI::HTTP http://www.google.com>
2.2.1 :046 > response = Net::HTTP.get_response(uri)
=> #<Net::HTTPOK 200 OK readbody=true>
2.2.1 :047 > response.code
=> "200"
2.2.1 :048 > uri = URI('http://www.google.com/this_does_not_exist')
=> #<URI::HTTP http://www.google.com/this_does_not_exist>
2.2.1 :049 > response = Net::HTTP.get_response(uri)
=> #<Net::HTTPNotFound 404 Not Found readbody=true>
2.2.1 :050 > response.code
=> "404"
2.2.1 :051 > uri = URI('http://www.google.com')
=> #<URI::HTTP http://www.google.com>
2.2.1 :052 > response = RestClient.get('http://www.google.com')
=> <RestClient::Response 200 "<!doctype h...">
2.2.1 :053 > response.code
=> 200
2.2.1 :054 > response = RestClient.get('http://www.google.com/this_does_not_exist')
RestClient::NotFound: 404 Not Found
from /Users/piperwarrior/.rvm/gems/ruby-2.2.1/gems/rest-client-2.0.0/lib/restclient/abstract_response.rb:223:in `exception_with_response'
from /Users/piperwarrior/.rvm/gems/ruby-2.2.1/gems/rest-client-2.0.0/lib/restclient/abstract_response.rb:103:in `return!'
from /Users/piperwarrior/.rvm/gems/ruby-2.2.1/gems/rest-client-2.0.0/lib/restclient/request.rb:860:in `process_result'
from /Users/piperwarrior/.rvm/gems/ruby-2.2.1/gems/rest-client-2.0.0/lib/restclient/request.rb:776:in `block in transmit'
from /Users/piperwarrior/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/net/http.rb:853:in `start'
from /Users/piperwarrior/.rvm/gems/ruby-2.2.1/gems/rest-client-2.0.0/lib/restclient/request.rb:766:in `transmit'
from /Users/piperwarrior/.rvm/gems/ruby-2.2.1/gems/rest-client-2.0.0/lib/restclient/request.rb:215:in `execute'
from /Users/piperwarrior/.rvm/gems/ruby-2.2.1/gems/rest-client-2.0.0/lib/restclient/request.rb:52:in `execute'
from /Users/piperwarrior/.rvm/gems/ruby-2.2.1/gems/rest-client-2.0.0/lib/restclient.rb:67:in `get'
from (irb):54
from /Users/piperwarrior/.rvm/rubies/ruby-2.2.1/bin/irb:11:in `<main>'
2.2.1 :055 >
2.2.1:045>uri=uri('http://www.google.com')
=> #
2.2.1:046>response=Net::HTTP.get\u响应(uri)
=> #
2.2.1:047>响应代码
=> "200"
2.2.1:048>uri=uri('http://www.google.com/this_does_not_exist')
=> #
2.2.1:049>response=Net::HTTP.get\u响应(uri)
=> #
2.2.1:050>响应代码
=> "404"
2.2.1:051>uri=uri('http://www.google.com')
=> #
2.2.1:052>响应=RestClient.get('http://www.google.com')
=>
2.2.1:053>响应代码
=> 200
2.2.1:054>响应=RestClient.get('http://www.google.com/this_does_not_exist')
RestClient::NotFound:404未找到
from/Users/piperwarrior/.rvm/gems/ruby-2.2.1/gems/rest-client-2.0.0/lib/restclient/abstract\u response.rb:223:in'exception\u with\u response'
from/Users/piperwarrior/.rvm/gems/ruby-2.2.1/gems/rest-client-2.0.0/lib/restclient/abstract_response.rb:103:“返回!”
from/Users/piperwarrior/.rvm/gems/ruby-2.2.1/gems/rest-client-2.0.0/lib/restclient/request.rb:860:在“过程\结果”中
from/Users/piperwarrior/.rvm/gems/ruby-2.2.1/gems/rest-client-2.0.0/lib/restclient/request.rb:776:在“传输中的块”中
from/Users/piperwarrior/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/net/http.rb:853:in'start'
from/Users/piperwarrior/.rvm/gems/ruby-2.2.1/gems/rest-client-2.0.0/lib/restclient/request.rb:766:in“传输”
from/Users/piperwarrior/.rvm/gems/ruby-2.2.1/gems/rest-client-2.0.0/lib/restclient/request.rb:215:在“执行”中
from/Users/piperwarrior/.rvm/gems/ruby-2.2.1/gems/rest-client-2.0.0/lib/restclient/request.rb:52:in'execute'
from/Users/piperwarrior/.rvm/gems/ruby-2.2.1/gems/rest-client-2.0.0/lib/restclient.rb:67:in'get'
来自(irb):54
from/Users/piperwarrior/.rvm/rubies/ruby-2.2.1/bin/irb:11:in`'
2.2.1 :055 >
来自:
- 对于介于
和200
之间的结果代码,将返回207
RestClient::Response
- 对于结果代码
、301
或302
,如果请求是307
或GET
HEAD
- 对于结果代码
,将遵循重定向并将请求转换为303
GET
- 对于其他情况,将引发持有响应的
;将针对已知错误代码引发特定的异常类RestClient::Exception
- 对异常调用
,以获取服务器的响应.response
e.response
检索响应对象