首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

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。

代码语言:javascript
复制
# File lib/net/http/response.rb, line 19
def body_permitted?
  self::HAS_BODY
end

私有类方法

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

代码语言:javascript
复制
# 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

代码语言:javascript
复制
# 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

代码语言:javascript
复制
# 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

返回完整的实体主体。

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

代码语言:javascript
复制
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
}
代码语言:javascript
复制
# File lib/net/http/response.rb, line 227
def body
  read_body()
end

另外别名为:entity

body=(value) Show source

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

代码语言:javascript
复制
# File lib/net/http/response.rb, line 233
def body=(value)
  @body = value
end

entity()

别名为:body

inspect() Show source

代码语言:javascript
复制
# 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对象第二次或以后的时间调用此方法将返回已读取的值。

代码语言:javascript
复制
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
}
代码语言:javascript
复制
# 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错误。

代码语言:javascript
复制
# File lib/net/http/response.rb, line 129
def value
  error! unless self.kind_of?(Net::HTTPSuccess)
end

私有实例方法

procdest(dest, block) Show source

代码语言:javascript
复制
# 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

代码语言:javascript
复制
# 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

代码语言:javascript
复制
# File lib/net/http/response.rb, line 331
def stream_check
  raise IOError, 'attempt to read body out of block' if @socket.closed?
end

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com