Net::HTTP 获取源代码和状态

编程入门 行业动态 更新时间:2024-10-25 15:20:01
本文介绍了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 获取源代码和状态

本文发布于:2023-11-15 19:03:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1597144.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:源代码   状态   Net   HTTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!