非常教程

Ruby 2.4参考手册

Net::HTTP

Net::HTTPResponse

Parent:ObjectIncluded modules:Net::HTTPHeader

HTTP响应类。

这个类将响应头和响应体(请求的实体)包装在一起。

它混合在HTTPHeader模块中,该模块通过类似散列的方法并通过单独的读取器提供对响应头值的访问。

请注意,每个可能的HTTP响应代码都定义了它自己的HTTPResponse子类。这些在下面列出。

所有类都在Net模块下定义。缩进表示继承。有关这些类的列表,请参阅Net :: HTTP。

常量

CODE_CLASS_TO_OBJ CODE_TO_OBJ

属性

codeR

HTTP结果代码字符串。例如,'302'。您还可以通过检查响应对象的哪个响应子类是一个实例来确定响应类型。

decode_contentRW

当请求没有包含来自用户的Accept-Encoding标头时,自动设置为true。

http_versionR

服务器支持的HTTP版本。

messageR

服务器发送的HTTP结果消息。例如,“未找到”。

msgR

服务器发送的HTTP结果消息。例如,“未找到”。

uriR

用于获取此响应的URI。响应URI仅在使用URI来创建请求时才可用。

公共类方法

body_permitted?() Show source

如果响应具有正文,则为true。

# File lib/net/http/response.rb, line 19
def body_permitted?
  self::HAS_BODY
end

私有类方法

each_response_header(sock) { |key, value| ... } Show source

# File lib/net/http/response.rb, line 51
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

read_status_line(sock) Show source

# File lib/net/http/response.rb, line 38
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

response_class(code) Show source

# File lib/net/http/response.rb, line 45
def response_class(code)
  CODE_TO_OBJ[code] or
  CODE_CLASS_TO_OBJ[code[0,1]] or
  Net::HTTPUnknownResponse
end

公共实例方法

body() Show source

返回完整的实体主体。

第二次或以后调用此方法将返回已读取的字符串。

http.request_get('/index.html') {|res|
  puts res.body
}

http.request_get('/index.html') {|res|
  p res.body.object_id   # 538149362
  p res.body.object_id   # 538149362
}
# File lib/net/http/response.rb, line 227
def body
  read_body()
end

另外别名为:entity

body=(value) Show source

因为它可能有必要修改主体,例如,解压这种方法有利于这一点。

# File lib/net/http/response.rb, line 233
def body=(value)
  @body = value
end

entity()

别名为:body

inspect() Show source

# File lib/net/http/response.rb, line 106
def inspect
  "#<#{self.class} #{@code} #{@message} readbody=#{@read}>"
end

read_body(dest = nil, &block) Show source

获取远程HTTP服务器返回的实体主体。

如果给出了一个块,那么主体将被传递给块,并且从插槽读入主体时,主体将被分段提供。

对同一个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 195
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

  @body
end

value() Show source

如果响应不是2xx(成功),则引发HTTP错误。

# File lib/net/http/response.rb, line 129
def value
  error! unless self.kind_of?(Net::HTTPSuccess)
end

私有实例方法

procdest(dest, block) Show source

# File lib/net/http/response.rb, line 335
def procdest(dest, block)
  raise ArgumentError, 'both arg and block given for HTTP method' if
    dest and block
  if block
    Net::ReadAdapter.new(block)
  else
    dest || ''
  end
end

read_body_0(dest) Show source

# File lib/net/http/response.rb, line 281
def read_body_0(dest)
  inflater do |inflate_body_io|
    if chunked?
      read_chunked dest, inflate_body_io
      return
    end

    @socket = inflate_body_io

    clen = content_length()
    if clen
      @socket.read clen, dest, true   # ignore EOF
      return
    end
    clen = range_length()
    if clen
      @socket.read clen, dest
      return
    end
    @socket.read_all dest
  end
end

stream_check() Show source

# File lib/net/http/response.rb, line 331
def stream_check
  raise IOError, 'attempt to read body out of block' if @socket.closed?
end

Net::HTTP相关

1.Net::HTTP::Copy
2.Net::HTTP::Delete
3.Net::HTTP::Get
4.Net::HTTP::Head
5.Net::HTTP::Lock
6.Net::HTTP::Mkcol
7.Net::HTTP::Move
8.Net::HTTP::Options
9.Net::HTTP::Patch
10.Net::HTTP::Post
11.Net::HTTP::Propfind
12.Net::HTTP::Proppatch
13.Net::HTTP::Put
14.Net::HTTP::Trace
15.Net::HTTP::Unlock
16.Net::HTTPAccepted
17.Net::HTTPBadGateway
18.Net::HTTPBadRequest
19.Net::HTTPClientError
20.Net::HTTPConflict
21.Net::HTTPContinue
22.Net::HTTPCreated
23.Net::HTTPExceptions
24.Net::HTTPExpectationFailed
25.Net::HTTPFailedDependency
26.Net::HTTPForbidden
27.Net::HTTPFound
28.Net::HTTPGatewayTimeOut
29.Net::HTTPGenericRequest
30.Net::HTTPGone
31.Net::HTTPHeader
32.Net::HTTPIMUsed
33.Net::HTTPInformation
34.Net::HTTPInsufficientStorage
35.Net::HTTPInternalServerError
36.Net::HTTPLengthRequired
37.Net::HTTPLocked
38.Net::HTTPMethodNotAllowed
39.Net::HTTPMovedPermanently
40.Net::HTTPMultipleChoices
41.Net::HTTPMultiStatus
42.Net::HTTPNetworkAuthenticationRequired
43.Net::HTTPNoContent
44.Net::HTTPNonAuthoritativeInformation
45.Net::HTTPNotAcceptable
46.Net::HTTPNotFound
47.Net::HTTPNotImplemented
48.Net::HTTPNotModified
49.Net::HTTPOK
50.Net::HTTPPartialContent
51.Net::HTTPPaymentRequired
52.Net::HTTPPermanentRedirect
53.Net::HTTPPreconditionFailed
54.Net::HTTPPreconditionRequired
55.Net::HTTPProxyAuthenticationRequired
56.Net::HTTPRedirection
57.Net::HTTPRequest
58.Net::HTTPRequestedRangeNotSatisfiable
59.Net::HTTPRequestEntityTooLarge
60.Net::HTTPRequestHeaderFieldsTooLarge
61.Net::HTTPRequestTimeOut
62.Net::HTTPRequestURITooLong
63.Net::HTTPResetContent
64.Net::HTTPSeeOther
65.Net::HTTPServerError
66.Net::HTTPServiceUnavailable
67.Net::HTTPSuccess
68.Net::HTTPSwitchProtocol
69.Net::HTTPTemporaryRedirect
70.Net::HTTPTooManyRequests
71.Net::HTTPUnauthorized
72.Net::HTTPUnavailableForLegalReasons
73.Net::HTTPUnknownResponse
74.Net::HTTPUnprocessableEntity
75.Net::HTTPUnsupportedMediaType
76.Net::HTTPUpgradeRequired
77.Net::HTTPUseProxy
78.Net::HTTPVersionNotSupported
Ruby 2.4

Ruby 是一种面向对象、命令式、函数式、动态的通用编程语言,是世界上最优美而巧妙的语言。

主页 https://www.ruby-lang.org/
源码 https://github.com/ruby/ruby
版本 2.4
发布版本 2.4.1

Ruby 2.4目录

1.缩略 | Abbrev
2.ARGF
3.数组 | Array
4.Base64
5.基本对象 | BasicObject
6.基准测试 | Benchmark
7.BigDecimal
8.绑定 | Binding
9.CGI
10.类 | Class
11.比较 | Comparable
12.负责 | Complex
13.计算续体 | Continuation
14.覆盖 | Coverage
15.CSV
16.日期 | Date
17.日期时间 | DateTime
18.DBM
19.代理 | Delegator
20.摘要 | Digest
21.Dir
22.DRb
23.编码 | Encoding
24.枚举 | Enumerable
25.枚举 | Enumerator
26.ENV
27.ERB
28.错误 | Errors
29.Etc
30.期望值 | Exception
31.错误类 | FalseClass
32.Fiber
33.Fiddle
34.文件 | File
35.文件实用程序 | FileUtils
36.查找 | Find
37.浮点 | Float
38.Forwardable
39.GC
40.GDBM
41.GetoptLong
42.Hash
43.Integer
44.IO
45.IPAddr
46.JSON
47.Kernel
48.语言 | 3Language
49.记录 | Logger
50.编排 | Marshal
51.MatchData
52.数学 | Math
53.矩阵 | Matrix
54.方法 | Method
55.模型 | Module
56.监控 | Monitor
57. 互斥 | Mutex
58.Net
59.Net::FTP
60.Net::HTTP
61.Net::IMAP
62.Net::SMTP
63.NilClass
64.数字 | Numeric
65.对象 | Object
66.ObjectSpace
67.Observable
68.Open3
69.OpenSSL
70.OpenStruct
71.OpenURI
72.OptionParser
73.路径名 | Pathname
74.完整输出 | PrettyPrint
75.Prime
76.Proc
77.过程 | Process
78.PStore
79.PTY
80.队列 | Queue
81.随机 | Random
82.范围 | Range
83.合理的 | Rational
84.Readline
85.Regexp
86.Resolv
87.Ripper
88.RubyVM
89.Scanf
90.SDBM
91.SecureRandom
92.Set
93.Shell
94.信号 | Signal
95.Singleton
96.套接字 | Socket
97.字符串 | String
98.StringIO
99.StringScanner
100.结构 | Struct