- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有一个 url 列表,我需要检查以下哪些 url 是有效的。
我使用的代码是
require 'net/http'
url = 'http://mysite.com'
res = Net::HTTP.get_response(URI.parse(url.to_s))
puts res.code
在这里,我可以检查响应代码 200 以获取有效的 url。我担心返回的“res”对象包含代码、正文等。因此我的响应(res 对象)变得很重。有什么办法让我只能得到响应代码。我不需要任何其他信息。请帮忙
最佳答案
我没有检查是否可以使用 Net::HTTP,但您可以使用 Curb,它是 curl 的 Ruby 包装器。看Curl::Easy#http_head
对于 Net::HTTP,您还可以使用 HTTP#head
,它使用 HEAD 方法从服务器请求 header 。
关于 HTTP 方法 HEAD 的信息:
9.4 HEAD
The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. This method can be used for obtaining metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification.
关于ruby - 如何仅从 Ruby 中的 HTTP 请求获取响应代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9699755/
我是一名优秀的程序员,十分优秀!