本文介绍了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('badurlexample')) => #<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 >更多推荐
Net::HTTP 获取源代码和状态
发布评论