Net::HTTP 获取源代码和状态
问题描述:
我目前正在使用以下方法获取页面的源代码:
I am currently getting the source code of the page using:
Net::HTTP.get(URI.parse(page.url))
我还想获取 HTTP 状态,而无需发出第二个请求.
I would also like to get the HTTP status, without making a second request.
有没有办法用另一种方法来做到这一点?我一直在查看文档,但似乎找不到我要找的内容.
Is there a way to do that with another method? I've been looking at the documentation, but cannot seem to find what I am looking for.
答
抱歉,其实已经解决了 :).
Sorry, actually figured it out :).
ruby-1.9.2-p136 :004 > r = Net::HTTP.get_response(URI.parse('http://badurlexample.com'))
=> #<Net::HTTPInternalServerError 500 Internal Server Error readbody=true>
ruby-1.9.2-p136 :005 > r.inspect
=> "#<Net::HTTPInternalServerError 500 Internal Server Error readbody=true>"
ruby-1.9.2-p136 :006 > r.body
=> "1 Errors:\r\nLine: 40 - ; expected"
ruby-1.9.2-p136 :007 >