如何仅从Ruby中的HTTP请求获取响应代码

时间:2022-10-09 07:27:36

I have a list of urls, I need to check which of the following urls are valid.

我有一个网址列表,我需要检查以下哪个网址有效。

The code I used is

我使用的代码是

require 'net/http'

url = 'http://mysite.com'
res = Net::HTTP.get_response(URI.parse(url.to_s))
puts res.code

Here I can check the response code 200 for a valid url. My concern is the 'res' object returned contains code, body, etc. So my response (res object) becomes heavy. Is there any way so that I can get only the response code. I don't need any other info. Please help

在这里,我可以检查响应代码200是否有效的网址。我关心的是返回的'res'对象包含代码,正文等。所以我的响应(res对象)变得很重。有什么方法可以让我只得到响应代码。我不需要任何其他信息。请帮忙

4 个解决方案

#1


8  

I didn't check if it's possible to do with Net::HTTP, but you can use Curb, which is the Ruby wrapper for curl. Look at Curl::Easy#http_head

我没有检查是否可以使用Net :: HTTP,但你可以使用Curb,它是curl的Ruby包装器。看看Curl :: Easy#http_head

With Net::HTTP you can also use HTTP#head, which requests headers from the server using the HEAD method.

使用Net :: HTTP,您还可以使用HTTP#head,它使用HEAD方法从服务器请求标头。

Information about HTTP's method HEAD:

有关HTTP方法的信息HEAD:

9.4 HEAD

9.4头

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.

HEAD方法与GET相同,只是服务器不能在响应中返回消息体。响应HEAD请求的HTTP头中包含的元信息应该与响应GET请求时发送的信息相同。该方法可用于获得关于请求所暗示的实体的元信息,而无需转移实体主体本身。此方法通常用于测试超文本链接的有效性,可访问性和最近的修改。

To get the response code of a page:

要获取页面的响应代码:

require 'net/http'
response = nil
Net::HTTP.start('www.example.com', 80) {|http|
  response = http.head('/page.html')
}
puts response.code

#2


6  

This is easiest in Faraday:

这在法拉第最简单:

# one line to make request
response = Faraday.head url

# example with headers
resource_size = response.headers['Content-Length']

#3


5  

The code I used is:

我使用的代码是:

response = nil
Net::HTTP.start('upload.wikimedia.org', 80) {|http|
 response = http.head(url)
}
puts response.code

#4


2  

A HEAD request could look like this:

HEAD请求可能如下所示:

require 'socket'

s = TCPSocket.open("google.com", 80)
s.puts "HEAD / HTTP/1.1"
s.puts "Host: google.com"
s.puts

headline = s.gets
s.close

status = headline.scan(/\d\d\d/).first.to_i

#1


8  

I didn't check if it's possible to do with Net::HTTP, but you can use Curb, which is the Ruby wrapper for curl. Look at Curl::Easy#http_head

我没有检查是否可以使用Net :: HTTP,但你可以使用Curb,它是curl的Ruby包装器。看看Curl :: Easy#http_head

With Net::HTTP you can also use HTTP#head, which requests headers from the server using the HEAD method.

使用Net :: HTTP,您还可以使用HTTP#head,它使用HEAD方法从服务器请求标头。

Information about HTTP's method HEAD:

有关HTTP方法的信息HEAD:

9.4 HEAD

9.4头

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.

HEAD方法与GET相同,只是服务器不能在响应中返回消息体。响应HEAD请求的HTTP头中包含的元信息应该与响应GET请求时发送的信息相同。该方法可用于获得关于请求所暗示的实体的元信息,而无需转移实体主体本身。此方法通常用于测试超文本链接的有效性,可访问性和最近的修改。

To get the response code of a page:

要获取页面的响应代码:

require 'net/http'
response = nil
Net::HTTP.start('www.example.com', 80) {|http|
  response = http.head('/page.html')
}
puts response.code

#2


6  

This is easiest in Faraday:

这在法拉第最简单:

# one line to make request
response = Faraday.head url

# example with headers
resource_size = response.headers['Content-Length']

#3


5  

The code I used is:

我使用的代码是:

response = nil
Net::HTTP.start('upload.wikimedia.org', 80) {|http|
 response = http.head(url)
}
puts response.code

#4


2  

A HEAD request could look like this:

HEAD请求可能如下所示:

require 'socket'

s = TCPSocket.open("google.com", 80)
s.puts "HEAD / HTTP/1.1"
s.puts "Host: google.com"
s.puts

headline = s.gets
s.close

status = headline.scan(/\d\d\d/).first.to_i