Net::HTTP 获取源代码和状态 [英] Net::HTTP get source code and status

查看:61
本文介绍了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 > 

这篇关于Net::HTTP 获取源代码和状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆