class Net::HTTPResponse
此类是 Net::HTTP 响应类的基类。
关于示例¶ ↑
此处的示例假设已 require 了 net/http
(这也 require 了 uri
)
require 'net/http'
此处的许多代码示例使用这些示例网站
一些示例还假设这些变量
uri = URI('https://jsonplaceholder.typicode.com/') uri.freeze # Examples may not modify. hostname = uri.hostname # => "jsonplaceholder.typicode.com" path = uri.path # => "/" port = uri.port # => 443
以便示例请求可以写成
Net::HTTP.get(uri) Net::HTTP.get(hostname, '/index.html') Net::HTTP.start(hostname) do |http| http.get('/todos/1') http.get('/todos/2') end
一个需要修改 URI
的示例,首先复制 uri
,然后修改副本
_uri = uri.dup _uri.path = '/todos/1'
返回的响应¶ ↑
方法 Net::HTTP.get_response
返回 Net::HTTPResponse 的子类之一的实例
Net::HTTP.get_response(uri) # => #<Net::HTTPOK 200 OK readbody=true> Net::HTTP.get_response(hostname, '/nosuch') # => #<Net::HTTPNotFound 404 Not Found readbody=true>
方法 Net::HTTP#request
也是如此
req = Net::HTTP::Get.new(uri) Net::HTTP.start(hostname) do |http| http.request(req) end # => #<Net::HTTPOK 200 OK readbody=true>
类 Net::HTTPResponse 包含模块 Net::HTTPHeader
,它提供通过以下方式(以及其他方式)访问响应头值的功能:
-
类似 Hash 的方法
[]
。 -
特定的读取器方法,例如
content_type
。
示例
res = Net::HTTP.get_response(uri) # => #<Net::HTTPOK 200 OK readbody=true> res['Content-Type'] # => "text/html; charset=UTF-8" res.content_type # => "text/html"
响应子类¶ ↑
类 Net::HTTPResponse 为每个 HTTP 状态码都有一个子类。您可以查找给定代码的响应类
Net::HTTPResponse::CODE_TO_OBJ['200'] # => Net::HTTPOK Net::HTTPResponse::CODE_TO_OBJ['400'] # => Net::HTTPBadRequest Net::HTTPResponse::CODE_TO_OBJ['404'] # => Net::HTTPNotFound
您可以检索响应对象的状态码
Net::HTTP.get_response(uri).code # => "200" Net::HTTP.get_response(hostname, '/nosuch').code # => "404"
响应子类(缩进显示类层次结构)
-
Net::HTTPUnknownResponse
(用于未处理的 HTTP 扩展)。 -
-
Net::HTTPContinue
(100) -
Net::HTTPSwitchProtocol
(101) -
Net::HTTPProcessing
(102) -
Net::HTTPEarlyHints
(103)
-
-
-
Net::HTTPOK
(200) -
Net::HTTPCreated
(201) -
Net::HTTPAccepted
(202) -
Net::HTTPNoContent
(204) -
Net::HTTPResetContent
(205) -
Net::HTTPPartialContent
(206) -
Net::HTTPMultiStatus
(207) -
Net::HTTPAlreadyReported
(208) -
Net::HTTPIMUsed
(226)
-
-
-
Net::HTTPMultipleChoices
(300) -
Net::HTTPFound
(302) -
Net::HTTPSeeOther
(303) -
Net::HTTPNotModified
(304) -
Net::HTTPUseProxy
(305)
-
-
-
Net::HTTPBadRequest
(400) -
Net::HTTPUnauthorized
(401) -
Net::HTTPPaymentRequired
(402) -
Net::HTTPForbidden
(403) -
Net::HTTPNotFound
(404) -
Net::HTTPNotAcceptable
(406) -
Net::HTTPRequestTimeOut
(408) -
Net::HTTPConflict
(409) -
Net::HTTPGone
(410) -
Net::HTTPLengthRequired
(411) -
Net::HTTPLocked
(423) -
Net::HTTPUpgradeRequired
(426) -
Net::HTTPTooManyRequests
(429)
-
-
-
Net::HTTPNotImplemented
(501) -
Net::HTTPBadGateway
(502) -
Net::HTTPGatewayTimeOut
(504) -
Net::HTTPLoopDetected
(508) -
Net::HTTPNotExtended
(510)
当出现协议错误时,还会引发 Net::HTTPBadResponse 异常。
常量
- CODE_CLASS_TO_OBJ
- CODE_TO_OBJ
属性
返回由 body_encoding
= 设置的值,如果没有设置则返回 false
;请参阅 body_encoding=
。
HTTP 结果代码字符串。例如,'302'。您还可以通过检查响应对象是哪个响应子类的实例来确定响应类型。
当请求不包含来自用户的 Accept-Encoding 标头时,自动 Set
为 true。
服务器支持的 HTTP 版本。
在读取带有指定 Content-Length 标头的正文时是否忽略 EOF。
服务器发送的 HTTP 结果消息。例如,“Not Found”。
公共类方法
源代码
# File lib/net/http/response.rb, line 138 def body_permitted? self::HAS_BODY end
如果响应有正文,则为 true。
私有类方法
源代码
# File lib/net/http/response.rb, line 170 def each_response_header(sock) key = value = nil while true line = sock.readuntil("\n", true).sub(/\s+\z/, '') break if line.empty? if line[0] == ?\s or line[0] == ?\t and value value << ' ' unless value.empty? value << line.strip else yield key, value if key key, value = line.strip.split(/\s*:\s*/, 2) raise Net::HTTPBadResponse, 'wrong header line format' if value.nil? end end yield key, value if key end
源代码
# File lib/net/http/response.rb, line 157 def read_status_line(sock) str = sock.readline m = /\AHTTP(?:\/(\d+\.\d+))?\s+(\d\d\d)(?:\s+(.*))?\z/in.match(str) or raise Net::HTTPBadResponse, "wrong status line: #{str.dump}" m.captures end
源代码
# File lib/net/http/response.rb, line 164 def response_class(code) CODE_TO_OBJ[code] or CODE_CLASS_TO_OBJ[code[0,1]] or Net::HTTPUnknownResponse end
公共实例方法
源代码
# File lib/net/http/response.rb, line 400 def body read_body() end
返回字符串响应正文;请注意,对未修改的正文重复调用会返回缓存的字符串
path = '/todos/1' Net::HTTP.start(hostname) do |http| res = http.get(path) p res.body p http.head(path).body # No body. end
输出
"{\n \"userId\": 1,\n \"id\": 1,\n \"title\": \"delectus aut autem\",\n \"completed\": false\n}" nil
源代码
# File lib/net/http/response.rb, line 405 def body=(value) @body = value end
将响应的正文设置为给定的值。
源代码
# File lib/net/http/response.rb, line 253 def body_encoding=(value) value = Encoding.find(value) if value.is_a?(String) @body_encoding = value end
设置读取正文时应使用的编码
-
如果给定值是
Encoding
对象,则将使用该编码。 -
否则,如果该值是字符串,则将使用 Encoding#find(value) 的值。
-
否则,将从正文本身推断出编码。
示例
http = Net::HTTP.new(hostname) req = Net::HTTP::Get.new('/') http.request(req) do |res| p res.body.encoding # => #<Encoding:ASCII-8BIT> end http.request(req) do |res| res.body_encoding = "UTF-8" p res.body.encoding # => #<Encoding:UTF-8> end
源代码
# File lib/net/http/response.rb, line 262 def inspect "#<#{self.class} #{@code} #{@message} readbody=#{@read}>" end
源代码
# File lib/net/http/response.rb, line 355 def read_body(dest = nil, &block) if @read raise IOError, "#{self.class}\#read_body called twice" if dest or block return @body end to = procdest(dest, block) stream_check if @body_exist read_body_0 to @body = to else @body = nil end @read = true return if @body.nil? case enc = @body_encoding when Encoding, false, nil # Encoding: force given encoding # false/nil: do not force encoding else # other value: detect encoding from body enc = detect_encoding(@body) end @body.force_encoding(enc) if enc @body end
获取远程 HTTP 服务器返回的实体正文。
如果给定了块,则将正文传递给该块,并且正文以片段的形式提供,因为它从套接字读取。
如果给定了 dest
参数,则将响应读取到该变量中,并使用 dest#<<
方法(它可以是 String
或 IO
,或任何其他响应 <<
的对象)。
第二次或后续调用同一 HTTPResponse
对象上的此方法将返回已读取的值。
http.request_get('/index.html') {|res| puts res.read_body } http.request_get('/index.html') {|res| p res.read_body.object_id # 538149362 p res.read_body.object_id # 538149362 } # using iterator http.request_get('/index.html') {|res| res.read_body do |segment| print segment end }
源代码
# File lib/net/http/response.rb, line 285 def value error! unless self.kind_of?(Net::HTTPSuccess) end
如果响应不是 2xx (成功),则引发 HTTP 错误。